1

I am trying to play with bash and arrays. But executing a sample script, I got an unexpected syntax error message: example.sh: 3: example.sh: Syntax error: "(" unexpected. And this is the script

#!/bin/bash
array=( one two three )

If I run the script with ./example.sh it works and no errors are displayed. But if I run sh example.sh I get the error message.

I thought that these two commands are the same:

  • sh example.sh
  • ./example.sh

so ... what is the difference between the two?

Peter Tutervai
  • 793
  • 12
  • 21
sensorario
  • 20,262
  • 30
  • 97
  • 159
  • http://superuser.com/questions/97614/what-exactly-is-the-sh-command – Hanky Panky Jun 18 '15 at 06:31
  • sh example.sh calls the /bin/sh executable which may or may not be the same as /bin/bash on your system. Compare the output of sh --version and bash --version for more details. – Ben T Jun 18 '15 at 06:42
  • In either case, I'm not sure it ought to make a difference. Does quoting your string values make a difference? – Ben T Jun 18 '15 at 06:44
  • I am just a "junior" bash developer. I did not thought that `sh` or `bash` has different meaning o_0 – sensorario Jun 18 '15 at 06:45

3 Answers3

3

When you launch it via ./example.sh then the command specified in the first line of the script is used to interpret the content. So your script executes in a bash, where such syntax is allowed for arrays.

When you launch it via sh example.sh then sh is the command that is used to interpret the content of the file. sh is the original Unix shell (aka Bourne shell) and this shell is a little more rude than bash (Bourne again shell). You don't have such arrays. Note that in sh the first line of your script is just interpreted as a comment.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
3

by using sh example.sh - you are specifying what shell interpreter to use for that script. Example being "bash example.sh" instead of "sh example.sh" etc etc.

Running scripts this way disregards the "shebang (#!/bin/bash)" that you have specified inside of the script. Since you wrote a bash script but are trying to run it as just "sh", this is why it is failing

by using ./example.sh, - You are specifying to run the script from your current directory. This will attempt to run the script in whatever shell you are currently in unless a shebang is specified. Since you have a "shebang" specified to run the script in bash... this is why it is working.

blue
  • 31
  • 1
2
array_name=(value1 ... valuen)

This is how to initializes an array in bash only. When you execute ./example.sh, the shebang line #!/bin/bash tells the system to use bash to execute.

However, when you execute sh example.sh, sh is used to execute. In many Unix systems (like Linux), sh is equivalent to bash. It seems sh is a different shell on your system.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • sh is different from bash (I didnt though). thank you very much. Mmm can you suggest me a good source to study all this stuffs? I think I cant find good information source alone: I dont know the answer. Any book? – sensorario Jun 18 '15 at 06:43