Zombo
Zombo

Reputation: 1

Why will script work with /bin/bash but not /bin/sh?

I am trying to understand why the script will work with #!/bin/bash but not #!/bin/sh. I am running Cygwin and both sh.exe and bash.exe seem to be identical (same file size).

$ cat 1.sh
#!/bin/sh
while read line; do
  echo ${line:0:9}
done < <(help | head -5)

$ ./1.sh
./1.sh: line 4: syntax error near unexpected token `<'
./1.sh: line 4: `done < <(help | head -5)'

$ cat 2.sh
#!/bin/bash
while read line; do
  echo ${line:0:9}
done < <(help | head -5)

$ ./2.sh
GNU bash,
These she
Type `hel
Use `info
Use `man

Upvotes: 0

Views: 3286

Answers (2)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84443

The Problems

  1. Bash is a superset of the Bourne shell, so many things are possible in Bash that aren't possible in more limited shells.
  2. Even when sh is a hardlink to bash, it behaves differently when invokes as sh. Many features supported by the Bash shell will not work in this mode.

The Solutions

  1. Remove your bashisms from the script, including nifty features like the process substitution you have on line 4.
  2. Run your script as Bash, not vanilla Bourne.

Upvotes: 2

Oleg V. Volkov
Oleg V. Volkov

Reputation: 22481

Despite being same file, shell analyzes its own name when run and switches to either plain shell or bash mode.

Upvotes: 5

Related Questions