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

5
  • 1
    What you use sh instead of bash, most bash extensions are disabled, including this one. Commented Apr 4, 2016 at 21:25
  • 1
    If your sh is a symbolic link to bash, read gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode Commented Apr 4, 2016 at 21:31
  • 1
    What do you mean "I am a student and therefore I can only run the bash terminal"? If you have access to a compiler, then you can build whatever shell you want! Doing so is an excellent exercise. Commented Apr 4, 2016 at 22:06
  • K, i will do some research into this! Thanks! :) Commented Apr 4, 2016 at 22:08
  • Maybe also read stackoverflow.com/tags/bash/info Commented Apr 7, 2016 at 5:41

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

10 Comments

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: stackoverflow.com/a/733901/58803). mywiki.wooledge.org/BashGuide/CommandsAndArguments#Scripts in-ulm.de/~mascheck/various/shebang
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.
@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.)
@pythontamer: "Will this solve my problem?" -- Did you try it?
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.
|
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

Comments

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.

1 Comment

So if i enter bash myscript.sh it will execute the code which contains the function [[:?
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 [.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.