Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

Suppose you have an alias go, but want it to do different things in different directories?

In one directory it should run cmd1, but in another directory it should run cmd2

By the way, I have an aliases for switching to the above directories already, so is it possible to append the go alias assignment to the foo alias?

alias "foo=cd /path/to/foo"

Working in bash(?) on OSX.

share|improve this question
This is what makefiles are for. – itsbruce Oct 29 '12 at 14:41
up vote 7 down vote accepted

It is not completely sure what you are asking, but an alias just expands to what is in the alias. If you have two aliases, you can append the different commands, even aliases.

alias "foo=cd /path/to/foo; go"
alias "foo2=cd /path/to/foo2; go"

In any other situation, you could specify a function in your .bashrc

function go ()
    if [ "$PWD" == "/path/to/foo" ]; then
    elif [ "$PWD" == "/path/to/go" ]; then

In case you have more choices, you could better use a case structure.

share|improve this answer

I get the feeling you are very "directory oriented", and, in that case, this might suite your mentality better.

(But to be honest, I think this is a bad idea altogether, you'd like commands to be global.)

In .bashrc, put alias go="./.cmd" (then source ~/.bashrc).

Then, in each such directory, put a script called .cmd, then chmod +x .cmd, then just go do your thing.

share|improve this answer
That's a lot of work...here's the use case: When running tests, I want to alias rspec-all to run certain tests. But which tests depends on the project. And that is determined by the directory. – B Seven Oct 27 '12 at 3:11
What I can see, you have two options, either use one function (like @Bernhard described above), and branch depending on directory (just like he said), or, you could put the parameters in the directories. Come to think of it, it doesn't have to be scripts, you could just put a file (touch .TESTNO; echo 2 > .TESTNO) in each directory, and then, in your function, look for that file, and branch according to what test number the .TESTNO file contains. Although not at all difficult, you probably cannot use an alias for this; you must use a function. – Emanuel Berg Oct 27 '12 at 5:17
function go() {
  if [ ! -e "./.cmd1" ]
    then command go -a $1
    else command go -b $1

Similar thing, but assuming "go" is a command you want different functionality from in a specific directory.

share|improve this answer

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.