HeadlinesBriefing HeadlinesBriefing.com

Tilde in PATH Not Expanding to HOME Directory

Hacker News •
×

A common pitfall in shell configuration is using a tilde (~) inside quoted strings when setting the PATH variable. When users add entries like `export PATH="$PATH:~/.local/bin/"` to their `.bashrc` or `.zshrc`, the tilde does not expand to the home directory. Instead, it is treated as a literal character, resulting in a relative path `./~/.local/bin/` being added to the environment variable. This occurs because tilde expansion only happens on unquoted words starting with `~`.

To resolve this, users should explicitly use the `$HOME` variable, such as `export PATH="$PATH:$HOME/.local/bin/"`. While the unquoted version `export PATH=$PATH:~/.local/bin` works in Bash and Zsh, it is fragile and can break if whitespace is introduced. The issue can be quickly diagnosed by running `echo "$PATH" | grep '~'`. If any output is returned, the configuration files should be updated to replace `~` with `$HOME`.

Source: Hacker News · Summarized by HeadlinesBriefing