I would like to create a symlink to ~/foo/ but when I write that, ~ is always resolved to the absolute home path. I've used ../s in the past and they work fine, is ~ not allowed?

The reason I'd like to do that is because ~ has a different absolute path depending on the machine and Linux/OSX, but always points to my home directory.

I'd like to include such a link in a git repo sync'd across several machines.

  • Maybe you should try to use the $HOME variable instead of ~. – Vinz Sep 29 '15 at 10:19
  • That's exactly what I don't want to do, because $HOME is always going to get resolved the moment I create the link (isn't it?), and is therefore going to be machine-dependent. – Sheljohn Sep 29 '15 at 10:21

The tilde character is expanded by the shell before the command is executed. It will be replaced by the value of $HOME. So the ln utility which creates the symlink will never see the tilde, only the full path. This path will be stored in the symlink.

In Linux, there is no otion to make a symlink variable. They are handled by the kernel. The kernel does not honor environment variables.

BSD allows such links. They are called variant symbolic links. You can do something like this:

$ ln -s ’${variable}’ linkname
$ ls -l linkname
lrwxrwxrwx 1 root root 6 Sep 29 12:33 linkname -> ’${variable}’

With varsym those variables can then be set:

varsym variable=value cat linkname
[...] contents of file with name value [...]

To address your question regarding symbolic links, "is ~ not allowed?".

Answer : ~ is not allowed. It'll allow you to create such a symlink but it won't resolve to the home directory when followed.

ln -s \~ d
ls -ld d
lrwxrwxrwx 1 steve steve 1 Sep 29 11:28 d -> ~
ls d
cd d
cd: d: No such file or directory
  • 1
    Alright then, I guess I'll just create one link for each repo. – Sheljohn Sep 29 '15 at 10:33
  • Any character except Nul and / is allowed in a UNIX pathname component. The ~ character on the other side is only expanded by certain shells if the character appears on the command line. So this answer is not helpful. – schily Sep 29 '15 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.