Wednesday, 14 August 2013

bash: propagating exit code of command executed in alias

bash: propagating exit code of command executed in alias

In bash, I'd like to propagate exit code of the particular command
executed in alias, so that alias call itself would return it (exit with
it).
Imagine the following example (Please note: command makes no sense, just
demonstrates problem I face):
alias cda='cd \/a; exit_code=$?; echo "STATUS: $exit_code"; alias cdb='cd
\/b'
cda && cdb
for me there are no such dirs as: /a and /b to make sure commands fail.
I'd like to execute cdb alias only in case cda alias execution succeeded.
However, as echo was the last command and it ended with exit status 0 =>
both are executed
What I've tried is:
alias cda='cd \/a; exit_code=$?; echo "STATUS: $exit_code"; exit
$exit_code'; alias cdb='cd \/b'
cda && cdb
however this exits the shell completely => not feasible for me.
Any idea how to propagate the exit status (in my case $exit_code) as the
alias exit status?

No comments:

Post a Comment