by waitwhat · 14y ago · 0 comments
$ cat foo.sh
#!/bin/bash
echo $BASH_VERSION
unset filename
if [[ $filename == "foo" ]];
$ ./foo.sh
4.1.2(1)-release
./foo.sh: line 5: syntax error: unexpected end of file
If $filename evaluates to an empty string, then the last line of code becomes if [[ == "foo" ]];
Similarly, if $filename evaluates to a b c, then the last line of code becomes if [[ a b c == "foo" ]];
The bash interpreter has problems with both of these.As a counterexample:
(echo $BASH_VERSION;
foo=foo; [[ $foo = foo ]] && echo $foo;
foo="1 2 3"; [[ $foo = "1 2 3" ]] && echo $foo;
foo=''; echo ${foo?"No foo for you"};
unset foo; echo ${foo?"No foo for you"})
4.2.10(1)-release
foo
1 2 3
bash: foo: No foo for you
That last one is one of the few ways you can tell whether a variable is unset or merely set to an empty string.lrn2bash.