Is there any reason to use the variable $HOME
instead of a simple ~
(tilde) in a shell script?
3 Answers
Tilde expansion doesn't work in some situations, like in the middle of strings like /foo/bar:~/baz
-
For another example of where it does not work, scroll to the answer by @no_allotment .– H2ONaClMay 28, 2017 at 1:10
Portability and uniformity of $HOME
. Tilde is a typing shortcut.
-
1So I'm correct in understanding that there are shells or systems in which tilde is not synonymous with $HOME? May 10, 2011 at 2:59
-
7@Michael yes, the default
sh
in Solaris 10 does not treat tilde as$HOME
neither is does expand~username
.– evolvahApr 2, 2012 at 3:44
Today I found tilde expansion doesn't work in double quotation(""
).
In zsh(sorry I didn't check the behavior of other shells.),
Inside double quotes (
""
), parameter and command substitution occur
Tilde expansion is a filename expansion, so it fails inside double quotation.
$HOME
is a parameter substitution and can be expanded inside double quotation.
$HOME
is a safe bet.