It would appear that ~ and $HOME are equivalent in command line and shell scripts. Is that true?
MacOS – Any difference between ~ and $HOME
command linemacos
Related Question
- The difference between .command, .tool, and .sh file extensions
- terminal – Understanding the Relationship Between ‘launchctl limit’ and ‘ulimit’
- MacOS – Difference between variable and pipe to shell in applescript
- MacOS – Difference between “Get Xcode” and “Install”
- Mac – What’s the difference between diskutil unmount and diskutil eject
- macOS Terminal – Difference Between Terminal Apps, CLI, CLT, and Prompt
- Replacement for CGSession –suspend
- The actual difference between sh and bash
Best Answer
It's true to a certain extent.
$HOME
is an expression for expansion of the environment variableHOME
~
(tilde) is a separate shell expansion component see footnoteWhen used as a command argument and in separation from other strings,
$HOME
and~
are usually equivalent.But there are cases where they differ:
if the string containing either is quoted, for example:
if they are concatenated to a string, for example:
dd if=${HOME}/source_file of=${HOME}/destination_file
will work.Shell will pass arguments
if=/Users/techraf/source_file
andof=/Users/techraf/destination_file
containing a valid path to thedd
command.dd if=~/source_file of=~/destination_file
won't workShell will pass arguments
if=~/source_file
andof=~/destination_file
to thedd
command and it will report an error as it does not interpret~
.footnote:
In fact
~
is by default replaced with the value ofHOME
, but ifHOME
is empty, it is resolved to a home directory:From
man bash
: