Bash/Tildeexpansion
Tildeexpansion
Beginnt der Wert mit einer unmaskierten Tilde (~), wird versucht, diese zu substituieren.
- Betrachtet werden alle der Tilde folgenden Zeichen bis zum ersten Schrägstrich (Slash).
- Ergibt dies eine gültige Benutzerkennung, so expandiert der Ausdruck zum Heimatverzeichnis dieses Benutzers.
Folgt der Tilde unmittelbar der Schrägstrich, wird der Ausdruck durch den Inhalt der Variablen HOME ersetzt; ist diese nicht gesetzt, wird das Heimatverzeichnis des aktuellen Benutzers angenommen:
$ var=~ $ echo $var $ /home/user $ var=~root/ $ echo $var $ /root/
Wenn ein Wort mit einer unmaskierten tilde Zeichen ("~") beninnt,
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 built-in invoked with the characters following tilde in the tilde-prefix as an argument.
- If the tilde-prefix, without 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 "=".
- 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.
Beispiel
$ export PATH="$PATH:~/testdir"
~/testdir will be expanded to $HOME/testdir, so if $HOME is /var/home/dirkwagner, the directory /var/home/dirkwagner/testdir will be added to the content of the PATH variable.