Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have this alias defined to run bash then navigate to a certain folder but when I run it, it only runs bash and stays in the current path/directory/folder.

I've defined them in two different ways in ~/.bashrc but both methods fail to navigate to the certain folder.

def #1

alias setup_ROR="bash; cd /users/nikeelevet/code/rails;" 

def #2

function setup_ROR() { 
        bash 
        cd /users/nikeelevet/code/rails 
}

Any idea as to why the cd isn't working?

Thanks!

share|improve this question

migrated from stackoverflow.com Oct 25 '11 at 2:29

This question came from our site for professional and enthusiast programmers.

    
Thanks for the help guys! Just realized how silly this question was haha. –  Waley Chen Oct 24 '11 at 1:18

5 Answers 5

up vote 2 down vote accepted

They both open a new shell, wait for it to close, then change directories. Both can be fixed by not trying to run bash.

share|improve this answer
    
Yep. @Waley, what's wrong with alias setup_ROR='cd /users/nikeelevet/code/rails' ? –  Aaron McDaid Oct 24 '11 at 1:07
    
bash is just another command. Like any command, when you invoke it, the shell waits for it to finish. –  Keith Thompson Oct 24 '11 at 1:10
    
Because that will cause the first command to end and the second to run. You're already in bash, which is why you have the aliases available in the first place; there's no need to try to run it again. –  Ignacio Vazquez-Abrams Oct 24 '11 at 1:11
    
haha @Aaron I just realized how silly this question is @_@ –  Waley Chen Oct 24 '11 at 1:12
    
It's not necessarily silly; sometimes it's perfectly sensible to want to launch a new shell. But if you don't need to, then yes, it's easier just to do the cd. –  Keith Thompson Oct 24 '11 at 1:38

Why not just:

alias setup_ROR="cd /users/nikeelevet/code/rails" 

And

function setup_ROR() { 
        cd /users/nikeelevet/code/rails 
}

If its in your bashrc, then your already in a bash shell when running the alias, why run another bash?

share|improve this answer

The alias is working. If you try this one:

~$alias test='ls;ls;'

Then run command test

~$test

You will see command 'ls' is run twice. The reason why you can't navigate into the folder is when it first executes command 'bash', the system will start a bash, which expects your input and will not return until you type 'exit'. I think if you run setup_ROR, then type 'exit', you will navigate into your directory.

I don't get the point why you want to run 'bash' in your alias.

share|improve this answer

I defer to @chown's answer, but if @Waley really requires a new bash instance, the following should suffice:

alias setup_ROR="pushd /users/nikeelevet/code/rails; bash; popd" 

This will start a new bash in the relevant directory, and then when the user exits that bash process, it will return to the original location

share|improve this answer

So what you're trying to do is start a new shell with a particular current directory, yes?

As it happens, the current working directory is one of the things that's inherited by new processes. So this should suit your purposes:

setup_ROR() {
    ( cd /users/nikeelevet/code/rails ; bash
}

The parentheses cause the two commands to be executed in a subshell, so the cd doesn't affect your current shell (as you'll see when you exit the subshell).

share|improve this answer

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.