linux - Why does /bin/sh behave differently to /bin/bash even if one points to the other? -
while playing around in shell investigating answer this question, noticed that, though /bin/sh
pointing /bin/bash
on system, 2 commands behave differently. first of all, output of
ls -lh /bin/sh
is:
lrwxrwxrwx 1 root root 4 apr 22 2013 /bin/sh -> bash*
however, invoking following command through /bin/sh
:
/bin/sh -c "script.sh 2> >( grep -v filter 2>&1 )"
returns error:
/bin/sh: -c: line 0: syntax error near unexpected token '>' /bin/sh: -c: line 0: 'script.sh 2> >( grep -v filter 2>&1 )'
while running same command through /bin/bash
:
/bin/bash -c "script.sh 2> >( grep -v filter 2>&1 )"
executes successfully, here output:
this should on stderr
for reference, here contents of script.sh
:
#!/bin/sh echo "filter: should filtered out" 1>&2 echo "this should on stderr" 1>&2 echo "filter: should filtered out" 1>&2
why 2 invocations behave differently?
bash
looks @ value of $argv[0]
(bash implemented in c) determine how invoked.
its behavior when invoked sh
documented in manual:
if bash invoked name
sh
, tries mimic startup behavior of historical versions ofsh
closely possible, while conforming posix standard well.when invoked interactive login shell, or non-interactive shell
-login
option, first attempts read , execute commands/etc/profile
,~/.profile
, in order.--noprofile
option may used inhibit behavior. when invoked interactive shell namesh
, bash looks variableenv
, expands value if defined, , uses expanded value name of file read , execute. since shell invokedsh
not attempt read , execute commands other startup files,--rcfile
option has no effect. non-interactive shell invoked namesh
not attempt read other startup files.when invoked
sh
, bash enters posix mode after startup files read
there's long list (currently 46 items) of things change when bash
in posix mode, documented here.
(posix mode useful way test scripts portability non-bash
shells.)
incidentally, programs change behavior depending on name under invoked common. versions of grep
, fgrep
, , egrep
implemented single executable (though gnu grep
doesn't this). view
typically symbolic link vi
or vim
; invoking view
causes open in read-only mode. busybox system includes number of individual commands symlinks master busybox
executable.
Comments
Post a Comment