I have some configuration in file config and would like to cat that file. However, sometimes config doesn't exist. In this case, I would like to have my command output a default value.

Perhaps something that worked like this:

$ ls
$ cat config || echo 42
$ echo 73 > config
$ cat config || echo 42

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.

  • Presuming of course that defaultconfig is guaranteed to exist. – DopeGhoti Feb 12 '16 at 18:11
  • The critical problem here is that it's the output of the commands that I care about. cat config gives an error cat: config: No such file or directory if config doesn't exist. So I end up getting the output of both commands instead of just the second one. – Cory Klein Feb 12 '16 at 19:23
  • 3
    Ah! I could pipe stderr to /dev/null and it works: cat config 2> /dev/null || echo 42. – Cory Klein Feb 12 '16 at 19:26

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
  • I'm not quite sure I understand how the trap logic would be combined with the cat config command from the example. Can you elaborate? – Cory Klein Feb 12 '16 at 19:25
  • $cat config -> "cat: config: No such file or directory\n" (from cat's error) "42" (from the trap's echo) – Jeff Schaller Feb 12 '16 at 19:31
if [ -a config ]; then cat config; else echo ${DEFAULT};fi

assuming the variable DEFAULT is set to something beforehand.

  • This appears to work for the cat config example, but I was hoping for a solution that works regardless of whether the failure is due to a missing file. For example ./get_config_from_web.sh || echo 42. – Cory Klein Feb 12 '16 at 17:19
  • does it have to be a one-liner ? – MelBurslan Feb 12 '16 at 17:22
  • Well... I could write a script that essentially does the same thing as above, but I was hoping that there was built-in bash syntax that supported this kind of a thing and I just hadn't learned about it yet. If no such bash syntax exists I guess the next best thing is a script, and the best script would be an elegant succinct one. – Cory Klein Feb 12 '16 at 17:24
  • I personally have not seen anything other than || and && constructs to determine the success or failure of an action in the same command space. But again with the enhancements in shell and me being an old timer, it might entirely be possible in a nook and cranny that eludes me and probably you as well – MelBurslan Feb 12 '16 at 17:28

You could use the exit code in an if statement:

cat config
if [[ "$?" -ne "0" ]]; then
    echo "VALUE"

If the previous command succeeds it will exit with a "0" value. So you are telling it that on any other exit (failures) it will output whatever is in the "VALUE".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.