2024-04-02 21:19:20 +02:00
|
|
|
---
|
|
|
|
tags:
|
|
|
|
- bash
|
|
|
|
- shell
|
|
|
|
- scripting
|
|
|
|
- expansion
|
|
|
|
- substitution
|
|
|
|
- tilde
|
|
|
|
- home
|
|
|
|
- homedir
|
|
|
|
- shortcut
|
|
|
|
---
|
2023-07-05 11:43:35 +02:00
|
|
|
|
2024-04-02 21:19:20 +02:00
|
|
|
# Tilde expansion
|
2023-07-05 11:43:35 +02:00
|
|
|
|
|
|
|
~
|
|
|
|
~/...
|
|
|
|
|
|
|
|
~NAME
|
|
|
|
~NAME/...
|
|
|
|
|
|
|
|
~+
|
|
|
|
~+/...
|
|
|
|
|
|
|
|
~-
|
|
|
|
~-/...
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2024-03-30 19:22:45 +01:00
|
|
|
If there's nothing to expand, i.e., in case of a wrong username or any
|
2023-07-05 11:43:35 +02:00
|
|
|
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`
|
|
|
|
|
2024-10-08 06:00:17 +02:00
|
|
|
!!! info "Note"
|
|
|
|
As of now (Bash 4.3-alpha) the following constructs
|
|
|
|
**also** works, though it's not a variable assignment:
|
2023-07-05 11:43:35 +02:00
|
|
|
|
2024-10-08 06:00:17 +02:00
|
|
|
echo foo=~
|
|
|
|
echo foo=:~
|
2023-07-05 11:43:35 +02:00
|
|
|
|
2024-10-08 06:00:17 +02:00
|
|
|
I don't know yet, if this is a bug or intended.
|
2023-07-05 11:43:35 +02:00
|
|
|
|
|
|
|
This way you can correctly use the tilde expansion in your
|
2024-01-29 02:07:56 +01:00
|
|
|
[PATH](../../syntax/shellvars.md#PATH):
|
2023-07-05 11:43:35 +02:00
|
|
|
|
|
|
|
PATH=~/mybins:~peter/mybins:$PATH
|
|
|
|
|
2024-10-08 06:00:17 +02:00
|
|
|
**Spaces in the referenced pathes?** A construct like...
|
2023-07-05 11:43:35 +02:00
|
|
|
|
|
|
|
~/"my directory"
|
|
|
|
|
2024-10-08 06:00:17 +02:00
|
|
|
...is perfectly valid and works!
|
2023-07-05 11:43:35 +02:00
|
|
|
|
|
|
|
## Home directory
|
|
|
|
|
|
|
|
~
|
|
|
|
~<NAME>
|
|
|
|
|
|
|
|
This form expands to the home-directory of the current user (`~`) or the
|
|
|
|
home directory of the given user (`~<NAME>`).
|
|
|
|
|
2024-03-30 20:09:26 +01:00
|
|
|
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
|
2023-07-05 11:43:35 +02:00
|
|
|
stays what it is. The requested home directory is found by asking the
|
|
|
|
operating system for the associated home directory for `<NAME>`.
|
|
|
|
|
|
|
|
To find the home directory of the current user (`~`), Bash has a
|
|
|
|
precedence:
|
|
|
|
|
2024-03-30 19:22:45 +01:00
|
|
|
- expand to the value of [HOME](../../syntax/shellvars.md#HOME) if it's
|
2023-07-05 11:43:35 +02:00
|
|
|
defined
|
|
|
|
- expand to the home directory of the user executing the shell
|
|
|
|
(operating system)
|
|
|
|
|
2024-10-08 06:00:17 +02:00
|
|
|
That means, the variable `HOME` can override the "real" home
|
2023-07-05 11:43:35 +02:00
|
|
|
directory, at least regarding tilde expansion.
|
|
|
|
|
|
|
|
## Current working directory
|
|
|
|
|
|
|
|
~+
|
|
|
|
|
2024-01-29 02:07:56 +01:00
|
|
|
This expands to the value of the [PWD](../../syntax/shellvars.md#PWD) variable,
|
2023-07-05 11:43:35 +02:00
|
|
|
which holds the currect working directory:
|
|
|
|
|
|
|
|
echo "CWD is $PWD"
|
|
|
|
|
|
|
|
is equivalent to (note it **must** be a separate word!):
|
|
|
|
|
|
|
|
echo "CWD is" ~+
|
|
|
|
|
|
|
|
## Previous working directory
|
|
|
|
|
|
|
|
~-
|
|
|
|
|
2024-01-29 02:07:56 +01:00
|
|
|
This expands to the value of the [OLDPWD](../../syntax/shellvars.md#OLDPWD)
|
2023-07-05 11:43:35 +02:00
|
|
|
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.
|
|
|
|
|
|
|
|
$ pwd
|
|
|
|
/home/bash
|
|
|
|
$ cd /etc
|
|
|
|
$ echo ~-
|
|
|
|
/home/bash
|
|
|
|
|
|
|
|
## See also
|
|
|
|
|
|
|
|
- Internal: [Introduction to expansion and
|
2024-01-29 02:07:56 +01:00
|
|
|
substitution](../../syntax/expansion/intro.md)
|