tags:

views:

50

answers:

3

Can we use $HOME or other environment variable in symbolic links?

I know about using relative paths ../../.config but sometimes are to many ../ :) something like ~/.config would be more comfortable, or use of $HOME.

Edit:

habbie's answer with psmears's comment is the answer, sorry my question was incomplete.

While (as other answers show) you can use environment variables when creating symbolic links (as with any shell command!), you can't actually have environment variable (or '~') references in the symlink itself

+4  A: 

Symbolic links are handled by the kernel, and the kernel does not care about environment variables. So, no.

Habbie
Not sure who modded this down without leaving a comment - the answer is correct. While (as other answers show) you can use environment variables when *creating* symbolic links (as with any shell command!), you can't actually have environment variable (or '~') references in the symlink itself.
psmears
thx that was the question.
kfl62
+2  A: 

yes. no problem. actually you won't actually be using the $HOME variable in your link, so it won't work with smart solutions for groups of users for example. The variable is translated by the shell when executing the command, and the content of the variable is used in the link.

ln -s ~/test /tmp/test 

is expaned to

/<path>/<to>/home/test -> /tmp/test

Ah. and only the environment variables of the person calling ln will work. You can't store other peoples environment variables in the link. The variables are expanded before calling the command.

thomasmalt
I just tried this on a Centos box and what thomasmalt writes is correct. The variable will be translated at the point of creating the symbolic link, and will not be dynamically updated if you change the environment variable.
kskjon
A: 

Yes you can.

ln -s $HOME/file/or/folder newname

You can set your own variables and use them, too. Add in your .bashrc (or .bash_profile):

export $MYPATH=/your/path
kogakure
Check out thomasmalt's answer. What you've said is correct in and of itself, but the question's about having the environment variable stored in the link such that the environment is consulted each time the link is followed (which is not possible).
Tony