I've just discovered a strange behaviour in bash
that I don't understand. The expression
${variable:=default}
sets variable
to the value default
if it isn't already set. Consider the following examples:
#!/bin/bash
file ${foo:=$1}
echo "foo >$foo<"
file ${bar:=$1} | cat
echo "bar >$bar<"
The output is:
$ ./test myfile.txt
myfile.txt: ASCII text
foo >myfile.txt<
myfile.txt: ASCII text
bar ><
You will notice that the variable foo
is assigned the value of $1
but the variable bar
is not, even though the result of its defaulting is presented to the file
command.
If you remove the innocuous pipe into cat
from line 4 and re-run it, then it both foo
and bar
get set to the value of $1
Am I missing somehting here, or is this potentially a bash
bug?
(GNU bash, version 4.3.30)
bash
feature, not abash
bug...