An alias is almost a textual substitution. So after your alias definition, typing doit
is equivalent to typing cd ~/some/dir; ./my_app; cd -
. This is a syntactically correct list of commands, and its return value is the return value of the last command in the sequence, here cd -
.
Other answers show a general way of saving the status of an intermediate command and returning it. However, in this specific case, there is a better way to express this sequence of actions: instead of changing to another directory and then changing back, execute the action that requires a different current directory inside a subshell, so that the current directory of the shell doesn't change at all.
doit () {
(cd ~/some/dir && ./my_app "$@")
}
I've made two additional improvements:
- Use
&&
rather than ;
between the commands, so as not to try to execute my_app
if the cd
command failed.
- Allow arguments to be passed to
my_app
: if you run doit foo bar
, my_app
is invoked with the two arguments foo
and bar
.