LinuxCommandLibrary

~

Reference current user's home directory

TLDR

List the current user's home directory contents

$ ls ~
copy

List the home directory contents of another user
$ ls ~[username]
copy

List the contents of the previous directory you were in
$ ls ~-
copy

SYNOPSIS

The tilde symbol itself does not accept options or arguments in the way a command does. Instead, it undergoes shell expansion based on the following patterns:

~
Expands to the current user's home directory.

~/path
Expands to the current user's home directory followed by the specified path.

~user/path
Expands to the home directory of the specified user, followed by the optional path.

DESCRIPTION

The ~ (tilde) symbol is not a traditional Linux command but a powerful shell expansion feature used to represent a user's home directory. When encountered by a shell (like Bash, Zsh, etc.), ~ is expanded into the absolute path of the current user's home directory before the command line is executed. This provides a concise and portable way to refer to the home directory without needing to know its full path (e.g., /home/username or /root).

Its primary use is in navigating the file system, accessing files, or specifying paths relative to the home directory. For instance, cd ~ will change the current directory to the user's home, and ls ~/documents will list the contents of the 'documents' subdirectory within the home directory. The tilde can also be followed by a specific username (e.g., ~john) to represent the home directory of that particular user, provided the user exists and their home directory is accessible. This mechanism simplifies scripting and interactive command-line operations, making paths more readable and less dependent on specific system configurations.

CAVEATS

The ~ character is expanded by the shell (e.g., Bash, Zsh) before the command is executed. If a command receives ~ as an argument without shell expansion, it will treat it as a literal character. Tilde expansion is typically enabled by default but can be disabled in some shell configurations or specific contexts (e.g., within single quotes '').

In scripts, it is generally recommended to use the $HOME environment variable for robustness and clarity, as it explicitly refers to the home directory of the user running the script, even if ~ expansion might yield the same result.

TILDE EXPANSION MECHANICS

Tilde expansion is one of several types of expansions performed by the shell. When a word begins with an unquoted tilde (~), the shell attempts to perform the expansion. If the tilde is not followed by a username, it expands to the value of the HOME shell variable. If it is followed by a username, the shell looks up the home directory of that user in the system's user database (e.g., /etc/passwd). If the username is invalid or the home directory cannot be found, the tilde remains unexpanded. Tilde expansion occurs relatively early in the shell's parsing process, before variable expansion, command substitution, and filename expansion.

TILDE VS. $HOME VARIABLE

While both ~ and $HOME refer to the user's home directory, they represent different mechanisms. $HOME is an environment variable that stores the full path to the home directory. It can be directly accessed and used in scripts and programs. ~ is a shell-specific shortcut that the shell interprets and expands to the value of $HOME (or another user's home directory). In interactive use, ~ is often preferred for its conciseness. In shell scripts, $HOME is often considered more robust because it explicitly references the variable, avoiding any potential ambiguity if tilde expansion rules were to change or be unexpectedly disabled in certain shell environments or specific contexts (e.g., if used within certain quoting scenarios where expansion might be suppressed).

HISTORY

The concept of the tilde (~) for home directory expansion originated in early Unix shells, specifically the C shell (csh) in the late 1970s. Its utility for simplifying path specification quickly led to its adoption by other popular shells, including the Korn Shell (ksh) and later the Bourne-Again Shell (bash) and Z Shell (zsh). It has since become a standard feature in POSIX-compliant shells, reflecting its enduring value in making command-line interactions more efficient and intuitive for users across various Unix-like operating systems.

SEE ALSO

cd(1), env(1), bash(1), zsh(1)

Copied to clipboard