Difference between $HOME and '~' (tilde)? Difference between $HOME and '~' (tilde)? linux linux

Difference between $HOME and '~' (tilde)?


The tilde is part of a shell expansion (like in bash, csh, zsh, etc). The $HOME variable is exportable and can be used independent of a specific shell.


The shell replaces ~ with the user's home directory (update: or perhaps by the home directory of some other user, if ~ is followed by something other than a /), but only if it's the first character of a word.

--with-libmemcached=~ has ~ not in the beginning, so the shell leaves it alone.


~ is expanded ONLY if it is the first character of a word AND it is unquoted

$ echo "~"~$ echo foo~foo~$ echo ~/home/guest$ echo ~/foo/home/guest/foo

~username is expanded to the HOME of the username.

$ echo ~root/root$ echo ~invaliduser~invaliduser

To quote filenames, you should use $HOME or quote the suffix

$ echo "$HOME/foo bar"/home/guest/foo bar$ echo ~/"foo bar"/home/guest/foo bar$ echo ~root/"foo bar"/root/foo bar

Note the following from "POSIX Tilde Expansion"

The pathname resulting from tilde expansion shall be treated as if quoted to prevent it being altered by field splitting and pathname expansion.