问题
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
回答1:
Despite being same file, shell analyzes its own name when run and switches to either plain shell or bash mode.
回答2:
The Problems
- Bash is a superset of the Bourne shell, so many things are possible in Bash that aren't possible in more limited shells.
- 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
- Remove your bashisms from the script, including nifty features like the process substitution you have on line 4.
- Run your script as Bash, not vanilla Bourne.
来源:https://stackoverflow.com/questions/11061727/why-will-script-work-with-bin-bash-but-not-bin-sh