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.

  • What's your terminal emulator?
    – daisy
    Mar 14, 2013 at 4:52
  • What is wrong with .bashrc? That is designed for these things!
    – Bernhard
    Mar 14, 2013 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. Mar 29, 2013 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, 2013 at 6:40

3 Answers 3


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)'
  • I think I will go with this approach. Thanks a lot! Mar 29, 2013 at 3:34
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
# if dir changes just repeat
echo 'PROJDIR="/home/me/path/to/other_project_dir"' > ~/.projdir
# and instantly this works from all shells / consoles:

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.