I guess 99% of programmers would either expect "Finished" to be printed or some syntax error.Well, 99% of shell programmers will (hopefully ;-) ) put a blank between "{" and "echo" in the line foo="$(testCode)" || {echo "foo";}
Yes, the interesting part is that depending on which space you accidentally forget, you'll either get the expected "Finished" or "bad code executed". foo="$(testCode)" || {echo "foo"; } # --> bad code foo="$(testCode)" || { echo "foo";} # --> Finished I guess it closes the function and {echo is interpreted as string or so, but that is probably not all (testCode is e.g. never executed). A syntax error would be nice instead. Shellcheck at least gets this unless you do something weird and more obvious such as ``` #!/bin/bash function badCode { echo "bad code executed" } function testCode { #pick some existing file echo "/etc/passwd" } function tfunc { local foo= foo="$(testCode)" || "{echo" "foo";} cat "$foo" || { badCode case $? in *) exit 1 esac } echo "Finished." ``` It's also interesting that this - in contrast to the original example - triggers a syntax error: ``` #!/bin/bash function badCode { echo "bad code executed" } function testCode { #pick some existing file echo "/etc/passwd" } function tfunc { local foo= foo="$(testCode)" || {echo "foo";} cat "$foo" || { badCode case $? in *) exit 1 esac } } #<-- only difference to the original example echo "Finished." ```
smime.p7s
Description: S/MIME Cryptographic Signature