Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The ~ (tilde) operator triggers tilde expansion, a built-in shell parsing mechanism where Bash replaces a tilde-prefixed string with a specific absolute directory path. This expansion occurs strictly before variable expansion, command substitution, and pathname expansion, provided the tilde is unquoted and positioned at the beginning of a word or immediately following an unquoted = or : in a variable assignment. During parsing, Bash identifies an unquoted ~ and reads all subsequent characters up to the first unquoted slash / (or the end of the word). This extracted string is evaluated as the tilde-prefix. If the tilde-prefix matches a known expansion rule, the shell substitutes the prefix with the corresponding path. If the prefix is invalid or unresolvable, the string is left completely unchanged.

Syntax and Expansion Variants

Current User Home Directory
~
~/path
An un-suffixed tilde expands to the value of the $HOME shell variable. If $HOME is unset, Bash falls back to the home directory of the user executing the shell, resolved via the operating system’s password database (e.g., getpwuid()). Specific User Home Directory
~username
~username/path
Expands to the absolute home directory associated with the specified username, resolved directly from the system’s password database (e.g., getpwnam()). Current Working Directory
~+
~+/path
Expands to the value of the $PWD shell variable, representing the current working directory. Previous Working Directory
~-
~-/path
Expands to the value of the $OLDPWD shell variable, representing the previous working directory. If $OLDPWD is unset, the expansion fails and the string remains literal. Directory Stack Expansion
~+N
~-N
Expands to specific entries within the shell’s directory stack (managed via pushd and popd).
  • ~+N expands to the string that would be displayed by the dirs +N command (the Nth directory from the left of the stack, zero-indexed).
  • ~-N expands to the string displayed by dirs -N (the Nth directory from the right of the stack, zero-indexed).

Assignment Context Parsing

Bash applies special parsing rules for tilde expansion during variable assignment. The expansion is triggered immediately following an unquoted = sign, and subsequently after any unquoted : characters within the assigned value. This is specifically designed to support PATH-like variable structures.

# Tilde expansion occurs after '=' and ':'
VAR=~/dir1:~+/dir2

Quoting and Suppression

Tilde expansion is strictly dependent on the ~ character remaining unquoted. If the tilde, or any character within the tilde-prefix, is enclosed in single quotes, double quotes, or escaped with a backslash, the expansion is suppressed and the tilde is treated as a literal character.

# Expansion suppressed
\~
"~"
'~'
~"username"
Master Bash with Deep Grasping Methodology!Learn More