TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
~ (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$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, resolved directly from the system’s password database (e.g., getpwnam()).
Current Working Directory
$PWD shell variable, representing the current working directory.
Previous Working Directory
$OLDPWD shell variable, representing the previous working directory. If $OLDPWD is unset, the expansion fails and the string remains literal.
Directory Stack Expansion
pushd and popd).
~+Nexpands to the string that would be displayed by thedirs +Ncommand (the Nth directory from the left of the stack, zero-indexed).~-Nexpands to the string displayed bydirs -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.
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.
Master Bash with Deep Grasping Methodology!Learn More





