I recently had a nice discussion on a mailinglist about #!/bin/bash vs #!/bin/sh as the
shebang line in shell scripts.
In general I support to use #!/bin/sh, maybe there are some corner-cases in which it makes sense to use #!/bin/bash, I am not a shell programmer.
But since the guy from
shellscripts.org (btw why shellscripts and not bashscripts?) told me that I have to use a bashism to
alter a variable outside the scope of a loop because of the created subshell and in my opinion this is a common misunderstanding I blog this.
foo=123
echo
456 | while read line
; do foo=$line
done
echo $foo
results in 123 because the loop has its own subshell and no access to foo outside of its scope.
foo=123
foo=$line
done < <( echo 456 )
echo $foo
is the proposed bashism to solve this issue, it will result in 456.
echo 456 | {
foo=123
foo=$line
done;}
echo $foo
Also results in 456 and is sh-compliant (or at-least runs in ksh93 which is the reference implementation for the
POSIX standard.
What also surprised me is that this is not running in bash, dash but in zsh
I reported this as a bug in bash and dash for
Debian, please correct me if I am wrong.