Is there any reason to use the variable $HOME
instead of a simple ~
(tilde) in a shell script?
Asked
Active
Viewed 7,836 times
40

Timur Shtatland
- 12,024
- 2
- 30
- 47

mybuddymichael
- 1,660
- 16
- 20
3 Answers
17
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? – mybuddymichael May 10 '11 at 02:59
-
7@Michael yes, the default `sh` in Solaris 10 does not treat tilde as `$HOME` neither is does expand `~username`. – evolvah Apr 02 '12 at 03:44
15
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.

no_allotment
- 151
- 1
- 6