Dommol
Dommol

Reputation: 191

Setting a pathname as an alias in the .bashrc file

I'm attempting to use aliases in the .bashrc file to store the paths I commonly go to (Ubuntu 14.04) i.e. alias pathname="/home/Dommol/test/next" But when I attempt to use the alias cd pathname I get an error -bash: cd: pathname: No such file or directory.

Question: How do I get bash to recognize that I am trying to use the alias pathname and not trying to change to to the directory pathname?

As an aside, I could make the alias alias pathname="cd /home/Dommol/test/next" and just type pathname to change

Upvotes: 0

Views: 1423

Answers (2)

anishsane
anishsane

Reputation: 20980

The answer by lurker should be the accepted solution. However, to answer your original question, I think, this could work:

#Your test code:
alias pathname="/home/Dommol/test/next"
cd pathname

#Similar functionality
ln -s /home/Dommol/test/next pathname
cd -P pathname

If you have more such directories & want to cd from any location, you could have this: (Note that this code below is limited to cd command.)

mkdir -p ~/.cdpath #Random name - could be changed
export CDPATH=~/.cdpath

ln -s /home/Dommol/test/next ~/.cdpath/pathname
cd -P pathname #Will work from any starting location.

ln -s /home/Dommol/test/next2 ~/.cdpath/pathname2
cd -P pathname2 #Will work from any starting location.

Best solution would be to create it as a variable, as already explained by lurker's answer. That solution will work for other commands as well.

Upvotes: 0

lurker
lurker

Reputation: 58244

alias is used to alias a command, not a shell variable. To do what you want, set a shell variable in your .bashrc:

pathname="/home/Dommol/test/next"

Then at the prompt:

$ cd $pathname

Using an alias to make a custom command with the arguments you want this in your .bashrc, as you noted in your "aside":

alias pathname="cd /home/Dommol/test/next"

Then at the prompt:

$ pathname

Upvotes: 3

Related Questions