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.

I'm currently using bash on Ubuntu, and I work a lot with a directory saved at /home/me/path/to/project_dir. It is a nuisance to change to the directory every time I open bash. So I'd like to save the path to a variable, say projdir, so that I can just do cd projdir. If my working directory changes, I should be able to change the path by overwriting the variable. I want to be able to update the variable easily, preferably with one command, without messing with .bashrc.

One important thing is that this variable should persist even if I restart bash. How do I do this? I'd still like to see my home directory when I start up bash, so changing the default starting directory to project_dir is not an option.

share|improve this question
    
What's your terminal emulator? –  warl0ck Mar 14 '13 at 4:52
    
What is wrong with .bashrc? That is designed for these things! –  Bernhard Mar 14 '13 at 6:29
    
@Bernhard, Sorry for getting back to you all really late. I was busy with other things. I'm not really an experienced Linux user, so I wanted to avoid messing with .bashrc. But, I understand that it is designed for these things, so I should probably get used to it. –  cookiemonster Mar 29 '13 at 3:33
    
@CookieMonster Yep, you should use .bashrc to your advantage, I am sure you will like it once you get use to it! Try to read up on aliases and functions :) –  Bernhard Mar 29 '13 at 6:40
add comment

3 Answers 3

up vote 1 down vote accepted

Have a look at CDPATH in man bash maybe that is already enough.

Otherwise define some alias in .bashrc. I would suggest:

alias setp='pwd >~/.projectdir'
alias gop='cd $(cat ~/.projectdir)'
share|improve this answer
    
I think I will go with this approach. Thanks a lot! –  cookiemonster Mar 29 '13 at 3:34
add comment
echo 'PROJDIR="/home/me/path/to/project_dir"' > ~/.projdir
# put the next in e.g. .bashrc
projdir () {
  . ~/.projdir
  cd "$PROJDIR"
}
# change into directory simply by
projdir
# if dir changes just repeat
echo 'PROJDIR="/home/me/path/to/other_project_dir"' > ~/.projdir
# and instantly this works from all shells / consoles:
projdir
share|improve this answer
add comment

Make a symbolic link in your home directory.

ln -s path/to/project_dir ~/p

Run cd p to switch to that project directory.

That's a bit of a cumbersome approach though. You could run one shell and switch to the desired directories, then start other shells from it, by running xterm & disown (replace xterm by whatever terminal emulator you prefer).

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.