In my Unix system, /bin/sh is symlinked to /bin/bash.
/bin/sh -> bash*
As this is the case, I expect that if an executable has a shebang of:
#!/bin/sh
it will be equivalent to:
#!/bin/bash
However, this is not true.
For example, below code uses process substitution which is supported by bash only:
#!/bin/bash
echo ${BASH_VERSION}
diff <(echo -e "1n2") <(echo -e "2n2")
Result:
4.2.46(2)-release
1c1
< 1
---
> 2
If I change the shebang to /bin/sh, as follows:
#!/bin/sh
echo ${BASH_VERSION}
diff <(echo -e "1n2") <(echo -e "2n2")
Result is:
4.2.46(2)-release
test.sh: line 3: syntax error near unexpected token `('
test.sh: line 3: `diff <(echo -e "1n2") <(echo -e "2n2")'
Moreover, even if remove the shebang, and run the script explicitly:
/bin/sh test.sh
I get the same error.
Weirdly (or maybe not), running the following will work:
`realpath /bin/sh` test.sh
I’m looking for an explanation for this behavior – as this is unexpected in my understanding, since /bin/sh -> bash. Also, the value of BASH_VERSION is consistent.
Thank you.
2
From the documentation:
When invoked as
sh
, Bash enters POSIX mode after reading the startup files.
This includes disabling bash
isms like process substitution.
2