1

When I execute my script sh myscript.sh I get an error message which states that [[: is an 'unexpected operator', however when i run my script in a bash emulator (http://www.tutorialspoint.com/execute_bash_online.php) it works and doesn't return this error. Furthermore, when i run the script using sh within the emulator it works and doesn't return the error even though on my server it would.

I've checked the link below and, from what i understand, i need to use the bash command. What is wrong with the sh command and how do i enable functions such as [[: to be executed?

NOTE: I am a student and therefore i can only run the bash terminal in school. So any help that will guarantee that this error will not be returned will be hugely appreciated.

[ :Unexpected operator in shell programming

Community
  • 1
  • 1
pythontamer
  • 109
  • 1
  • 10

4 Answers4

4

Simple answer is to just use bash myscript.sh. As has been mentioned below, the [[ syntax is bash specific, and not supported by sh.

These are two separate shells, each with their own variation on the scripting language. A vulgar analogy would be that bash is to sh, what c++ is to c. Bash has more features, and some easier syntax, but they share a lot in common.

If you have #!/bin/bash at the top of your file, then it's a bash script. You run this by entering bash yourscript.sh if it is not executable, or simply ./yourscript.sh if it is.

If you have #!/bin/sh, then it's an sh script. You run this by the same principles described above.

J.M. Janzen
  • 671
  • 1
  • 8
  • 19
  • How do I move it to `#!/bin.bash`? – pythontamer Apr 04 '16 at 21:29
  • 1
    The top of a script MUST have a #! line, called 'shebang' or 'hashbang'. It tells the kernel what program to run your script with. I recommend `#!/usr/bin/env bash` (why: http://stackoverflow.com/a/733901/58803). http://mywiki.wooledge.org/BashGuide/CommandsAndArguments#Scripts http://www.in-ulm.de/~mascheck/various/shebang/ – Rany Albeg Wein Apr 04 '16 at 21:31
  • So within my script writer, i should have the first line as `#!/usr/bin/env bash`? [I use mcedit as my script writer] – pythontamer Apr 04 '16 at 21:41
  • 1
    The whole point of the `#!` line is that you *don't* have to invoke it by typing `sh yourscript`. Just type `./yourscript`, and the system will figure how to run it. – Keith Thompson Apr 04 '16 at 21:42
  • @pythontamer: Either `#!/usr/bin/env bash` or `#!/bin/bash` should work. I prefer the latter. See [this question](http://unix.stackexchange.com/q/29608/10454) and [my answer](http://unix.stackexchange.com/a/29620/10454). – Keith Thompson Apr 04 '16 at 21:43
  • Will this solve my problem? Just to be clear, i enter this on the very first line of my script writer cause wouldn't it be interpreted as a comment? – pythontamer Apr 04 '16 at 21:44
  • 1
    @RanyAlbegWein: Having a `#!` line is certainly a good idea, but it's not mandatory. If it's missing, you can still execute the script by typing `sh scriptname` or `bash scriptname` explicitly. (As for the `#!/usr/bin/env` trick, see my previous comment.) – Keith Thompson Apr 04 '16 at 21:44
  • 1
    @pythontamer: "*Will this solve my problem?*" -- Did you try it? – Keith Thompson Apr 04 '16 at 21:45
  • 1
    I've added this to my question: "NOTE: I am a student and therefore i can only run the bash terminal in school. So any help that will guarantee that this error will not be returned will be hugely appreciated." Sorry for the inconvenience this may cause. – pythontamer Apr 04 '16 at 21:45
  • @pythontamer: `bash` is a shell, not a terminal. If you want to run the `sh` shell rather than bash, you should be able to type `sh` at a bash prompt, or for a script put `#!/bin/sh` on the first line. If you mean that your current assignment requires you to use bash, that's a different story, but there's no reason being a student should prevent you from running a different shell. – Keith Thompson Apr 04 '16 at 22:25
3

You could think about it like this:
There are many "human languages" (French, Japanese, English, Hindi etc)

There are many different "shell languages" (sh, csh, tcsh, zsh, bash etc)
Think of sh and bash as languages, not commands.

The errors you are getting is because your computer is expecting you to talk to it in sh, but actually you are talking to it in bash. It is like giving a French document to a German translator....

So, to resolve this, you just need to inform your computer that your script is written in bash.

To do this, simply add this line to the very top of your script file:

#!/bin/bash

Mtl Dev
  • 1,604
  • 20
  • 29
2

Many Linux distributions use a smaller, simpler shell implementation than Bash for their default sh binary. They do this for various reasons. If you need Bash, run bash explicitly.

Phil Miller
  • 36,389
  • 13
  • 67
  • 90
1

[[ is a Bash keyword similar to (but more powerful than) the [ command.

See

Unless you're writing for POSIX sh, it is recommended to use [[ instead of [.

Rany Albeg Wein
  • 3,304
  • 3
  • 16
  • 26