Is there any reason to use the variable $HOME
instead of a simple ~
(tilde) in a shell script?
$HOME is a Linux bash shell variable. It indicates the home directory of the current user; the default argument for the cd command. The value of this variable is also used when performing tilde expansion.
~/ (tilde slash) The tilde (~) is a Linux "shortcut" to denote a user's home directory. Thus tilde slash (~/) is the beginning of a path to a file or directory below the user's home directory. For example, for user01, file /home/user01/test.
Bash also performs tilde expansion on words satisfying the conditions of variable assignments (see Shell Parameters) when they appear as arguments to simple commands. Bash does not do this, except for the declaration commands listed above, when in POSIX mode.
If you execute ./script.sh , $0 will give output ./script.sh but if you execute it with bash script.sh it will give output script.sh . Show activity on this post. They are called the Positional Parameters.
Tilde expansion doesn't work in some situations, like in the middle of strings like /foo/bar:~/baz
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With