Skip to content
Advertisement

Why does bash behave differently, when it is called as sh?

I have an ubuntu machine with default shell set to bash and both ways to the binary in $PATH:

$ which bash
/bin/bash
$ which sh
/bin/sh
$ ll /bin/sh
lrwxrwxrwx 1 root root 4 Mar  6  2013 /bin/sh -> bash*

But when I try to call a script that uses the inline file descriptor (that only bash can handle, but not sh) both calls behave differently:

$ . ./inline-pipe
reached
$ bash ./inline-pipe
reached
$ sh ./inline-pipe
./inline-pipe: line 6: syntax error near unexpected token `<'
./inline-pipe: line 6: `done < <(echo "reached")'

The example-script I am referring to looks like that

#!/bin/sh
while read line; do
if [[ "$line" == "reached" ]]; then echo "reached"; fi
done < <(echo "reached")

the real one is a little bit longer:

#!/bin/sh
declare -A elements
while read line
do
    for ele in $(echo $line | grep -o "[a-z]*:[^ ]*")
    do
        id=$(echo $ele | cut -d ":" -f 1)
        elements["$id"]=$(echo $ele | cut -d ":" -f 2)
    done
done < <(adb devices -l)
echo ${elements[*]}

Advertisement

Answer

When bash is invoked as sh, it (mostly) restricts itself to features found in the POSIX standard. Process substitution is not one of those features, hence the error.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement