← All stories
● Covered by 1 source · 1 reportLow impact1 neutral

Tilde (~) in shell PATH variable does not expand to $HOME in quoted strings

🔄 Updated 27d ago
New to BrevFeed? We gather this story from every outlet covering it into one summary — ranked by real-world impact, not just the latest headline — so you never miss what matters. What is BrevFeed? →

Key points

  • Tilde expansion only occurs in unquoted shell inputs.
  • Quoted `~` in PATH creates a literal `~` directory.
  • Use `$HOME` instead of `~` in quoted PATH definitions.
  • This affects `.bashrc`, `.zshrc`, and `.profile` files.

The Tilde Expansion Issue

A common practice in shell configuration files like `.bashrc` or `.zshrc` is to add directories to the `PATH` environment variable. When using a tilde (`~`) to represent the home directory within a quoted string, for example `export PATH="$PATH:~/.local/bin/"`, the shell does not perform tilde expansion. This means `~` is treated as a literal character, resulting in a path like `./~/.local/bin/` being added to `PATH` instead of the intended `/home/<user>/.local/bin/`.

Why it Happens

Bash documentation specifies that tilde expansion occurs only in unquoted inputs. While Bash and Zsh do perform tilde expansion in unquoted variable assignments (e.g., `export PATH=$PATH:~/.local/bin`), relying on this is fragile due to potential issues with whitespace. The problem was identified when a sandboxing tool, `nono agent`, warned about `PATH` entries it could write to, specifically flagging `~/.local/bin/`.

Demonstration of the Problem

A practical example shows that if `PATH` is set to `"~/.local/bin/"`, and a directory `./~/.local/bin/` is created containing an executable named `kek`, the shell successfully finds and runs `kek` from this literal `~` directory. This confirms that the home directory was not involved in the path resolution, and a new directory structure was implicitly created.

The Solution

To correctly add a home-relative path to `PATH` within a quoted string, users should explicitly use the `$HOME` environment variable. The correct syntax is `export PATH="$PATH:$HOME/.local/bin/"`. This ensures that the home directory path is properly expanded before being added to `PATH`.

Checking for the Issue

Users can check their current `PATH` for this issue by running `echo "$PATH" | grep '~'` or `echo "$PATH" | tr ':' '\n' | grep '~'`. If any output contains `~`, it indicates a potential misconfiguration that should be corrected in `.bashrc`, `.zshrc`, or `.profile` files by replacing `~` with `$HOME`.

✨ This summary was generated by AI from the outlets' reporting listed below. It is not independently verified and may contain errors — check the original sources. How BrevFeed works →

The daily brief

One email each morning: the day's tech stories, clustered across outlets and summarized. No account needed.

One email a day. Unsubscribe in one click, any time.

Today's brief

Spend a few minutes, get the whole day. Every topic's top stories in one hands-free rundown — listen, watch, or read the transcript.

~4 min · 3 stories · Nov 07

▶ Play today's brief Listen on Spotify

New every morning, and the back catalogue is archived by date.

Reporting from

When defining the PATH environment variable in shell configuration files, using a tilde (~) within double quotes, such as `export PATH="$PATH:~/.local/bin/"`, prevents the tilde from expanding to the user's home directory. Instead, the shell interprets `~` literally, creating a relative path like `./~/.local/bin/` rather than `/home/<user>/.local/bin/`. This behavior can lead to unexpected directory structures and executable lookup issues.