Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So at my position we use a shared set of aliases to do builds. Something like...

alias 'build'='cd $BUILD_ROOT; m; cp ...; cp...; deploy; deploy; deploy; cd -'

This is super irritating because the make command takes about 3 minutes and I usually realize I missed something, hit Ctrl-C, and then immediately run build again. This leaves me in a directory I don't care to be in, with a painful path to retype.

What I'd really like is something like...

alias 'betterbuild'='pushd .; build; even-if-the-user-cancelled popd'

Is there a way to do this?

share|improve this question
I'ma head off the obvious answers to this: use a script. I really want to keep using the same command as the rest of the team so as they modify their scripts I keep up. Other aliases and scripts depend on this alias and I want those to continue to work. – Hounshell Aug 2 '13 at 16:26
This is exactly the sort of things scripts are meant for. – Michael Kjörling Aug 2 '13 at 16:27
Is the cp ... supposed to be cd ....? – terdon Aug 2 '13 at 17:59
@terdon: I think the dots represent missing text. – Evan Teitelman Aug 2 '13 at 18:21

You may be able to use a subshell.

alias build='( cd $BUILD_ROOT; m; cp ...; cp...; deploy; deploy; deploy; )'

In the above example, cd $BUILD_ROOT changes the current working directory of the subshell, but not the interactive shell that calls the alias. For a generic case, you can use a subshell and an exit trap.

alias build='( cleanup() { do; cleanup; }; trap cleanup EXIT; cd $BUILD_ROOT; m; cp ...; cp...; deploy; deploy; deploy; )'

Do reconsider using a script though. The above example would look much cleaner in a script. Scripts are interpreted in their own shell instances. The subshell syntax would not be necessary in a script.

You mentioned in a comment that scripts depend on your alias. This suggests that you are sourcing your scripts instead of executing them, which is generally a bad practice. Also, you mentioned that there are aliases that depend on your alias. You can use scripts within aliases.

share|improve this answer
More generally, processes are executed in a context separate from the invoking shell. A shell script just happens to be a slightly special kind of process invocation. – Michael Kjörling Aug 2 '13 at 16:34
@MichaelKjörling: Thanks. I modified that part of my answer. – Evan Teitelman Aug 2 '13 at 16:53

Well, I was going to say make it a script, because aliases aren't really meant to do complex things like this and scripts are meant for more-complex or longer-running tasks. Aliases are basically meant to map one command to another, for example to provide default parameters to a command that doesn't support that natively.

However, since you stated in a comment that you want to keep this an alias, we'll have to figure out how to do what you want within the boundaries of an alias.

The natural choice then becomes to launch a subshell from within the alias. For example:

~$ alias xyz1='( cd ~/tmp; sleep 10; cd ~ )'
~$ alias xyz2='cd ~/tmp; sleep 10; cd ~'
~$ xyz2
~/tmp$ cd
~$ xyz1

This changes the working directory only within the subshell, which exits when either the alias finishes executing normally, or is aborted e.g. through Ctrl+C.

share|improve this answer
Looks like you and Evan are having a "!$" moment 8-). – slm Aug 2 '13 at 16:57
@slm Yes, we did end up saying pretty much the same thing at pretty much the same time. I'll let the community decide who did better :) – Michael Kjörling Aug 2 '13 at 16:59
I did my part and +1'd you both 8-). – slm Aug 2 '13 at 17:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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