- publishing free software manuals
GNU Bash Reference Manual
by Chet Ramey and Brian Fox
Paperback (6"x9"), 180 pages
ISBN 0954161777
RRP £19.95 ($29.95)

"An essential resource .... the most detailed coverage available for all aspects of Bash" --- Linux User and Developer Magazine (Issue 37, Mar 2004) Get a printed copy>>>

3.5.2 Tilde Expansion

If a word begins with an unquoted tilde character (‘~’), all of the characters up to the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix. 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 HOME shell variable. 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 tilde-prefix is ‘~+’, the value of the shell variable PWD replaces the tilde-prefix. If the tilde-prefix is ‘~-’, the value of the shell variable OLDPWD, if it is set, is substituted.

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 characters following tilde in the tilde-prefix as an argument (see section 6.8 The Directory Stack). If the tilde-prefix, sans the tilde, consists of a number without a leading ‘+’ or ‘-’, ‘+’ is assumed.

If the login name is invalid, or the tilde expansion fails, the word is left unchanged.

Each variable assignment is checked for unquoted tilde-prefixes immediately following a ‘:’ or the first ‘=’. In these cases, tilde expansion is also performed. Consequently, one may use file names with tildes in assignments to PATH, MAILPATH, and CDPATH, and the shell assigns the expanded value.

The following table shows how Bash treats unquoted tilde-prefixes:

~
The value of $HOME
~/foo
‘$HOME/foo’
~fred/foo
The subdirectory foo of the home directory of the user fred
~+/foo
‘$PWD/foo’
~-/foo
‘${OLDPWD-'~-'}/foo’
~N
The string that would be displayed by ‘dirs +N
~+N
The string that would be displayed by ‘dirs +N
~-N
The string that would be displayed by ‘dirs -N
ISBN 0954161777GNU Bash Reference ManualSee the print edition