Bash Directory Alias – Set Alias on a Per-Directory Basis

aliasbashdirectoryfunction

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.

Best Answer

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
       cmd1
    elif [ "$PWD" == "/path/to/go" ]; then
       cmd2
    fi;
}

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

Related Question