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?

  • 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 at 8:29
  • 2
    export SOMEDIR="/var/www/site"; cd "$SOMEDIR/app" – RoVo Oct 22 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 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 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 at 8:52
up vote 11 down vote accepted

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

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

If there's a subdirectory of app called doc:

 $ cd app/doc
 /var/www/site/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:

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 ".:~:/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
/tmp
$ somedir
$ pwd
/var/www/site
$ somedir app
$ pwd
/var/www/site/app

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

somedir="/var/www/site"
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"
/var/www/site$ 

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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