Provide default value if command returns with non-zero exit code

Your construct is fine. You could even do someting like

cat config || cat defaultconfig

If you use some random command (like the ./get_config_from_web in comments), you'll have to make sure the command does give a sensible return status. That can be tricky, shell scripts just return the result of the last command executed, you'd have to do a exit if you want something else as result.


The following will echo 42 for any noncaught error condition:

trap "echo 42" ERR

You can make this a configurable variable:

trap 'echo "${CONFIG:=42}"' ERR  # if $CONFIG is not set, it will be defaulted to 42