Question

I'm kind of just realizing how powerful the terminal can be. My question is essentially if I can create an alias that just prints the name of a directory. For example, I could easily make an alias such as "alias sitename="cd ~/sites/path/to/my/site/". But what I want is an alias that only prints the directory name so that I can use it for several things. So that, for example, if I wanted I could just say cd "alias", or mv from-dir "alias".

Is there a way to do this? I've tried and it seems to recognize the alias if I just type it in: it will report "alias" is a directory. But if I try to couple it with another command, it fails.

Was it helpful?

Solution

You don't want to use alias, what you are after is an environment variable

$ export SITENAME="~/sites/path/to/my/site/"
$ cd $SITENAME

Bash is quite picky over syntax - note the lack of spaces in the export and the $ when you use it.

OTHER TIPS

Use a variable, simply

d=/path/to/some/directory
echo $d
cd $d
mv somedir $d/

You don't need to use an alias here, a variable is sufficient.

It sounds like you want to set a variable, not an alias. Such as, sitename=/home/jimbo/. Then, cd $sitename would put you at /home/jimbo/.

If you want this variable to have permanence (i.e. you don't have to set it every time you open a new session), then you can make it an environmental variable using the export command or add it to your .bashrc file (typically located at $HOME/.bashrc) using the line: sitename=/home/jimbo/.

FYI, $HOME is another environmental variable that's equivalent to ~/.

Using a variable is the simplest solution. You could get fancy and use an array:

mydir() { echo "/my/directory"; }

To display the value

mydir

To use the value, you need some extra puncuation

cd $(mydir)
cd `mydir`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top