LinuxCommandLibrary

tilde

TLDR

Home directory

$ cd ~
copy
Another user's home directory
$ cd ~[username]
copy
Previous directory
$ cd ~-
copy
Current directory
$ echo ~+
copy
Case modification (bash 4+)
$ echo ${var~~}
copy

SYNOPSIS

~
~username
~+
~-

DESCRIPTION

~ (tilde) is expanded by the shell to directory paths. It's a shorthand that avoids hardcoding paths like /home/user.
Tilde expansion only occurs at the start of a word or after :. In the middle of text, ~ is literal.

$ cd ~                    # Go home
cd ~/Documents          # Home subdirectory
cd ~root                # Root's home
PATH=$PATH:~/bin        # After : works
copy
The ~+ and ~- forms mirror the pushd/popd directory stack, allowing quick navigation between recent directories.

EXAMPLES

$ # Copy to home
cp file.txt ~

# Use another user's home
sudo cp config ~www-data/

# Toggle between directories
cd /var/log
cd /etc
cd ~-     # Back to /var/log
cd ~-     # Back to /etc

# Case toggle (bash 4+)
name="Hello"
echo ${name~~}    # hELLO
copy

TILDE EXPANSIONS

~

Current user's home directory ($HOME)
~username
Specified user's home directory
~+
Current working directory ($PWD)
~-
Previous working directory ($OLDPWD)

PARAMETER EXPANSION (bash 4+)

${var^}

Uppercase first character
${var^^}
Uppercase all characters
${var,}
Lowercase first character
${var,,}
Lowercase all characters
${var~}
Toggle case of first character
${var~~}
Toggle case of all characters

CAVEATS

Tilde only expands when unquoted and at word start. "~" is literal, not home.
~user looks up the user in the password database, not just string concatenation.
~- is empty if no previous directory (new shell).

SEE ALSO

cd(1), bash(1), pwd(1), pushd(1)

Copied to clipboard