My friend asked me why this two simple script are not working in both shells:
Test File "abc.txt":
aaa
111
bbb
111
ccc
111
ddd
Script #1 (a):
#!/bin/sh
while read -r line ; do
echo "Processing $line"
done < <(grep 111 abc.txt)
Output:
./a: line 4: syntax error near unexpected token `<'
./a: line 4: `done < <(grep 111 abc.txt)'
Script #2 (b):
#!/bin/bash
while read -r line ; do
echo "Processing $line"
done < <(grep 111 abc.txt)
Output:
Processing 111
Processing 111
Processing 111
I checked bash and sh on my machine and if I understand this correct this is the same thing.
/bin/sh is just a link to /bin/bash:
-rwxr-xr-x 1 root root 938736 May 10 2012 bash
lrwxrwxrwx. 1 root root 4 Feb 13 10:20 sh -> bash
Can someone explain me where is the difference?
It's because Bash behaves differently when
$0
issh
.From Bash's behaviour [Bash Hackers Wiki]:
More information can be found at the Bash Reference Manual: Bash POSIX Mode, specifically:
which is why your
sh
script is failing, as the<(..)
syntax is using process substitution.According to
it says
So you can not use bash features when run your script with
/bin/sh
.