bash: Tilde Expansion

1 
1 3.5.2 Tilde Expansion
1 ---------------------
1 
1 If a word begins with an unquoted tilde character ('~'), all of the
1 characters up to the first unquoted slash (or all characters, if there
1 is no unquoted slash) are considered a TILDE-PREFIX.  If none of the
1 characters in the tilde-prefix are quoted, the characters in the
1 tilde-prefix following the tilde are treated as a possible LOGIN NAME.
1 If this login name is the null string, the tilde is replaced with the
1 value of the 'HOME' shell variable.  If 'HOME' is unset, the home
1 directory of the user executing the shell is substituted instead.
1 Otherwise, the tilde-prefix is replaced with the home directory
1 associated with the specified login name.
1 
1    If the tilde-prefix is '~+', the value of the shell variable 'PWD'
1 replaces the tilde-prefix.  If the tilde-prefix is '~-', the value of
1 the shell variable 'OLDPWD', if it is set, is substituted.
1 
1    If the characters following the tilde in the tilde-prefix consist of
1 a number N, optionally prefixed by a '+' or a '-', the tilde-prefix is
1 replaced with the corresponding element from the directory stack, as it
1 would be displayed by the 'dirs' builtin invoked with the characters
11 following tilde in the tilde-prefix as an argument (⇒The Directory
 Stack).  If the tilde-prefix, sans the tilde, consists of a number
1 without a leading '+' or '-', '+' is assumed.
1 
1    If the login name is invalid, or the tilde expansion fails, the word
1 is left unchanged.
1 
1    Each variable assignment is checked for unquoted tilde-prefixes
1 immediately following a ':' or the first '='.  In these cases, tilde
1 expansion is also performed.  Consequently, one may use filenames with
1 tildes in assignments to 'PATH', 'MAILPATH', and 'CDPATH', and the shell
1 assigns the expanded value.
1 
1    The following table shows how Bash treats unquoted tilde-prefixes:
1 
1 '~'
1      The value of '$HOME'
1 '~/foo'
1      '$HOME/foo'
1 
1 '~fred/foo'
1      The subdirectory 'foo' of the home directory of the user 'fred'
1 
1 '~+/foo'
1      '$PWD/foo'
1 
1 '~-/foo'
1      '${OLDPWD-'~-'}/foo'
1 
1 '~N'
1      The string that would be displayed by 'dirs +N'
1 
1 '~+N'
1      The string that would be displayed by 'dirs +N'
1 
1 '~-N'
1      The string that would be displayed by 'dirs -N'
1