Reputation: 11070
Is there a concise, general, idiomatic bash construction that would force a statement to error when a subshell it invokes errors? For example,
cd $(git rev-parse --show-toplevel)
will invariably return 0
even if the git
command errors, which makes it difficult to script something like
cd $(git rev-parse --show-toplevel) && echo 'Success!'
Of course you can just do the following, but I was wondering if there was a better way:
DIR=$(git rev-parse --show-toplevel) && cd $DIR && echo 'Success!'
Upvotes: 3
Views: 90
Reputation: 39591
It's not quite a general solution, but in that example you could do:
cd $(git rev-parse --show-toplevel || echo -@) && echo 'Success!'
This solution works by turns the output into something that the command won't accept if the command in the substitution fails.
Upvotes: 1