Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have .bash_aliases with:

alias c1='cd /home/me/code/php'
alias c2='cd /home/me/code/jquery'
alias c3='cd /home/me/code/ruby'
alias c4='cd /home/me/code/c'

How can I DRY this up and loosen the coupling to that entire directory structure?

I have tried:

alias code_base='/home/me/code/'
alias c1='cd code_base/php'
alias c2='cd code_base/jquery'
alias c3='cd code_base/ruby'
alias c4='cd code_base/c'

but I get the error 'is a directory' when I try and use c1 through c4.

I also tried:

alias c1='cd `code_base`/php'

but that didn't work; it gave me:

bash: cd: /php: No such file or directory

I tried alias c1='cd '+work+'/php', but that gave me the directory error too.

share|improve this question
up vote 3 down vote accepted

Rather use a variable to define the base directory.

alias c1="cd $CODE_BASE/php"
alias c2="cd $CODE_BASE/jquery"
alias c3="cd $CODE_BASE/ruby"
alias c4="cd $CODE_BASE/c"
share|improve this answer
Note make sure to use "'s and not ''s – Michael Durrant Jun 7 '12 at 3:41
@MichaelDurrant You can't use ' here, because $CODE_BASE variable is used inside alias and ' won't let expand this variable. In general case, you can use either ' either ". Both work well. – rush Jun 7 '12 at 8:38
Single quotes are fine; substitution of the variable is just delayed until the alias is executed. – glenn jackman Jun 7 '12 at 9:35

Besides the answer from mgorven you can also use the CDPATH variable. From man bash:

   CDPATH The  search  path for the cd command.  This is a colon-separated
          list of directories in which the  shell  looks  for  destination
          directories  specified  by  the  cd  command.  A sample value is

If you'd set it to CDPATH=.:$HOME/code/ you can just run cd php and would change the directory to $HOME/code/php if the directory exists.

Another solution is to use hashed directories if you are using zsh, e.g:

hash -d code_base=/home/me/code/

Afterwards you can use cd ~code_base/directory to change to a specific directory.

share|improve this answer

Your Answer


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.