Twitchykid Twitchykid - 25 days ago 10
Linux Question

Use environment variable for part of full path

How would I create an environment variable (maybe that's not the right word?) to use as a shortcut for very long paths? However, I don't want it to be the full path. I know how to set the absolute path in bashrc but what I want is something like:

cd $MYPATH/further/down/path/


How do I setup
$MYPATH
to use in this way? Whenever I try to google for this all that comes back are aliases for the full path which I do not want.

edit #1) I want the path to be persistent for each new terminal session. So I am looking for a command to put in bashrc. doing
alias MYPATH=~/my/path
does not work. I'm sure there is a simple command that I'm just not aware of that does what I want.

Answer

This works exactly as you think it would. If you define

export MYPATH=/home/user

then you can use it like

cd $MYPATH/further/down/path/

In all shells started as a child of the shell where you defined the variable. If you want to make it persistent you would put it in profile or bashrc scripts, so it is set on each login.

NB: if you want to concatenate letters after the variable you would have to use this:

cd ${MYPATH}2/other

and finally, if it contains blanks it does not hurt to quote the variable (but dont use '' for this):

cd "${MYPATH}/test"