I have a little shell script that runs anything executable in a designated directory, while passing along two arguments:
#!/bin/sh
cd '/foo'
for s in bar/*;
do [ -x $s ] && $s "$1" "$2" || : ;
done
Is the || : bit serving any purpose? I wanted the executables to be kicked off asynchronously, so I changed the ; to a &, but is there any reason I shouldn’t just do:
#!/bin/sh
cd '/foo'
for s in bar/*;
do [ -x $s ] && $s "$1" "$2" &
done
|| :(which is often spelled|| true) means you don’t care whether the command succeeds or not. It forces a successful return status ($?).It is good practice to run shell scripts under the shell’s
-eoption so that errors don’t go unnoticed. This option can be either activated on the shebang line (#!/bin/sh -e) or in the script itself (set -e). It causes errors to abort execution of the script.Since in this case you are not running under
-e, the|| :doesn’t make a different because the shell will ignore the result code anyway if the command fails. Furthermore, you are running a command in the background in this case so checking the return code doesn’t even make sense.In general, including
|| trueeven if you are not running under-ecan be good for two reasons:-ein case it is ever changed to run under-e.