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?


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?

  • 4
    Even if aliases were expanded in operand positions this wouldn't do what you want, you'd end up with cd cd /var/www/site/app (note the double cd). Why not just use a variable?
    – Fox
    Oct 22, 2018 at 8:29
  • 2
    export SOMEDIR="/var/www/site"; cd "$SOMEDIR/app"
    – pLumo
    Oct 22, 2018 at 8:33
  • @Fox ah of course, can't believe I missed that ... could I add it to the alias, e.g. somedir/app? (tried: answer no xD)
    – treyBake
    Oct 22, 2018 at 8:34
  • @RoVo I suppose that makes sense, create it as a var and then can use within the cd - ty :)
    – treyBake
    Oct 22, 2018 at 8:35
  • Also look up cdable vars and autocd in the bash manual (not sure if they can be combined, but one of them maybe useful)
    – muru
    Oct 22, 2018 at 8:52

3 Answers 3


The bash shell has a CDPATH shell variable that helps you do this without an alias:

 $ CDPATH=".:/var/www/site"
 $ cd app

If there's a subdirectory of app called doc:

 $ cd app/doc

With a CDPATH value of .:/var/www/site, the cd 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:


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 ".:~:/usr".

Note that CDPATH should not be exported as you usually do not want this variable to affect bash scripts that you run from your interactive session.


Quoting a handy Bash reference (emphasis mine):

A Bash alias is essentially nothing more than a keyboard shortcut, an abbreviation, a means of avoiding typing a long command sequence. ... It would be nice if aliases could assume some of the functionality of the C preprocessor, such as macro expansion, but unfortunately Bash does not expand arguments within the alias body. Moreover, a script fails to expand an alias itself within "compound constructs," such as if/then statements, loops, and functions. An added limitation is that an alias will not expand recursively. Almost invariably, whatever we would like an alias to do could be accomplished much more effectively with a function.

Notwithstanding your special case of CDPATH, as Kusalananda answered, an equivalent function might look like:

somedir() {
    cd /var/www/site/"${1:-}"

Now you've got:

$ pwd
$ somedir
$ pwd
$ somedir app
$ pwd

Aliases don't work that way, but you could use a variable instead:

cd "$somedir/app"

With due credit to Fox and RoVo for suggesting this in the comments

And if you want to save a few keystrokes, you could set up an alias:

alias somedir='cd "$somedir"'

Or if you enable autocd, you can cd by just giving the variable like a command:

~$ shopt -s autocd
~$ "$somedir"

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.