Let's say I have this alias in my .bashrc
alias somedir="cd /var/www/site"
how can I use somedir
in say … a cd
command?
e.g.
cd somedir/app/
doing this currently returns:
-bash: cd: somedir/app: No such file or directory
Is it even possible to use an alias this way?
Best Answer
The
bash
shell has aCDPATH
shell variable that helps you do this without an alias:If there's a subdirectory of
app
calleddoc
:With a
CDPATH
value of.:/var/www/site
, thecd
command will first look in the current directory for the directory path given on the command line, and if none is found it will look under/var/www/site
.From the
bash
manual:Note that
CDPATH
should not be exported as you usually do not want this variable to affectbash
scripts that you run from your interactive session.