Take the 2-minute tour ×
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.

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
1  
This is what makefiles are for. –  itsbruce Oct 29 '12 at 14:41
add comment

3 Answers

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

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

share|improve this answer
add comment

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
1  
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
add comment
function go() {
  if [ ! -e "./.cmd1" ]
    then command go -a $1
    else command go -b $1
  fi
}

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

share|improve this answer
add comment

Your Answer

 
discard

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.