The tilde expansion replaces the tilde with a path value which is dependent of the syntax.
If the tilde expansion fails, the word is unchanged.
If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the tilde are treated as a possible login name. If this login name is:
- the null string, the tilde is replaced with the value of the shell parameter HOME. If HOME is unset, the home directory of the user executing the shell is substituted instead.
- Otherwise, the tilde-prefix is replaced with the home directory associated with the specified login name.
If the login name is invalid, the word is unchanged.
- Tilde with login name (deamon)
The HOME is defined in the file /etc/passwd
This expansion context permits to set:
If the tilde-prefix is a:
- ~+, the value of the shell variable PWD replaces the tilde-prefix.
- ~-, the value of the shell variable OLDPWD, if it is set, is substituted.
echo ~+ # PWD echo ~- # OLDPWD
If the characters following the tilde in the tilde-prefix consist of a number N, optionally prefixed by a ‘+’ or a ‘-’, the tilde-prefix is replaced with the corresponding element from the directory stack, as it would be displayed by the dirs builtin invoked with the tilde-prefix as an argument.
If the characters following the tilde in the tilde-prefix consist of a number without a leading + or -, + is assumed.
Consequently, one may use file names with tildes in assignments to:
- and CDPATH,
and the shell assigns the expanded value.