Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variables in symbolic links

Tags:

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

like image 236
kfl62 Avatar asked Oct 08 '10 08:10

kfl62


People also ask

What does symbolic link contain?

A symbolic link contains a text string that is automatically interpreted and followed by the operating system as a path to another file or directory. This other file or directory is called the "target". The symbolic link is a second file that exists independently of its target.

What characters are allowed in environment variables?

Only uppercase letters, lowercase letters, and underscores from this character set are allowed.

Can a symbolic link be a directory?

Symlink, also known as a symbolic link in Linux, creates a link to a file or a directory for easier access. To put it in another way, symlinks are links that points to another file or folder in your system, quite similar to the shortcuts in Windows.


2 Answers

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

like image 172
Habbie Avatar answered Sep 23 '22 20:09

Habbie


Even though the symbolic links are resolved by the kernel, you could still do a LD_PRELOAD trick, wrapping all libc functions that take pathnames and expand any $XYZ components in the string returned by 'readlink' (parameter expansion). Then feed the expanded path to the wrapped function. You have to escape the target path from shell expansion when creating the link, as jaztik suggests.

As the injected library has full access to the users' environment, this will fulfill all expectations of the OP.

like image 25
Ronald Hoogenboom Avatar answered Sep 24 '22 20:09

Ronald Hoogenboom