Reputation
3,095
Top tag
Next privilege 5,000 Rep.
Approve tag wiki edits
Badges
9 38 65
Impact
~364k people reached

Apr
29
awarded  Famous Question
Apr
21
awarded  Popular Question
Mar
24
awarded  Popular Question
Mar
5
awarded  Famous Question
Feb
13
awarded  Nice Question
Feb
12
accepted Provide default value if command returns with non-zero exit code
Feb
12
comment Provide default value if command returns with non-zero exit code
Ah! I could pipe stderr to /dev/null and it works: cat config 2> /dev/null || echo 42.
Feb
12
comment Provide default value if command returns with non-zero exit code
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?
Feb
12
comment Provide default value if command returns with non-zero exit code
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.
Feb
12
comment Provide default value if command returns with non-zero exit code
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.
Feb
12
comment Provide default value if command returns with non-zero exit code
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.
Feb
12
asked Provide default value if command returns with non-zero exit code
Feb
9
awarded  Popular Question
Jan
24
awarded  Yearling
Jan
20
awarded  Popular Question
Jan
20
awarded  Good Question
Dec
29
awarded  Notable Question
Dec
22
awarded  Notable Question
Dec
9
awarded  Popular Question
Dec
7
awarded  Favorite Question