The tilde expansion is used to expand to several specific pathnames:
* home directories
* current working directory
* previous working directory
Tilde expansion is only performed, when the tilde-construct is at the beginning of a word, or a separate word.
If there's nothing to expand, i.e., in case of a wrong username or any other error condition, the tilde construct is not replaced, it stays what it is.
Tilde expansion is also performed everytime a variable is assigned:
* after the **first** ''='': ''TARGET=~moonman/share''
* after **every** '':'' (colon) in the assigned value: ''TARGET=file:~moonman/share''
If the given user doesn't exist (or if his home directory isn't determinable, for some reason), it doesn't expand to something else, it stays what it is. The requested home directory is found by asking the operating system for the associated home directory for ''<NAME>''.
This expands to the value of the [[syntax:shellvars#OLDPWD|OLDPWD]] variable, which holds the previous working directory (the one before the last ''cd''). If ''OLDPWD'' is unset (never changed the directory), it is not expanded.