Problem
While investigating the solution to this issue in my shell, I realized that, despite the fact that /bin/sh points to /bin/bash on my system, the two commands work differently. First and foremost, the output of
ls -lh /bin/sh
is:
lrwxrwxrwx 1 root root 4 Apr 22 2013 /bin/sh -> bash*
Using /bin/sh, however, you can run the following command:
/bin/sh -c "script.sh 2> >( grep -v FILTER 2>&1 )"
returns this error:
/bin/sh: -c: line 0: syntax error near unexpected token '>'
/bin/sh: -c: line 0: 'script.sh 2> >( grep -v FILTER 2>&1 )'
When using /bin/bash to do the same command:
/bin/bash -c "script.sh 2> >( grep -v FILTER 2>&1 )"
If all goes well, here’s what you’ll get:
This should be on stderr
The contents of script.sh are listed here for your convenience:
#!/bin/sh
echo "FILTER: This should be filtered out" 1>&2
echo "This should be on stderr" 1>&2
echo "FILTER: This should be filtered out" 1>&2
What’s the difference between the two invocations?
Asked by Squirrel
Solution #1
To establish how it was invoked, bash examines the value of $argv[0] (bash is written in C).
Its behavior when called with the command sh is described in the manual:
There’s a big list of things that change when bash is in POSIX mode (currently 46 items), which is listed here.
(POSIX mode is most likely beneficial for testing scripts’ portability to non-bash shells.)
nge their behavior based on the name they were summoned under is pretty common. Some versions of grep, fgrep, and egrep (albeit not GNU grep) are combined into a single executable. When you invoke view as a symbolic link to vi or vim, it opens in read-only mode. The Busybox system comprises a number of separate commands, each of which is a symlink to the master busybox executable.
Answered by Keith Thompson
Solution #2
After reading the startup files that bash would typically read, invoking it as sh leads it to enter posix mode (as opposed to the startup files a POSIX sh would read.) There are a variety of invocation modes available in Bash. These modes are described in the INVOCATION section of the manual. Here’s more information about the POSIX mode.
This mode indicates that bash will attempt to conform to POSIX expectations to varying degrees. Bash supports a number distinct invocations for this mode, each with slightly different ramifications, as discussed here:
Answered by kojiro
Solution #3
The following is taken from the Bash Reference Manual:
Answered by Etan Reisner
Solution #4
Because the bash binary examines how it was invoked (via argv[0]) and enters compatibility mode if it is run as sh.
Answered by Carl Norum
Post is based on https://stackoverflow.com/questions/26717870/why-does-bin-sh-behave-differently-to-bin-bash-even-if-one-points-to-the-other