Don't Put Tilde In Your Path

Hacker News Top News

Summary

一篇技术博文指出在 shell 配置中用引号包裹的 `PATH="$PATH:~/.local/bin"` 不会展开波浪号,导致当前目录下的字面 `./~/` 路径被加入 PATH,带来潜在劫持风险;建议改用 `$HOME`,并介绍了 nono 沙箱工具的相关警告与排查方法。

No content available
Original Article
View Cached Full Text

Cached at: 10/11/26, 12:39 PM

# The tilde in your PATH may not be your HOME Source: [https://disconnect3d.pl/2026/10/02/dont-put-tilde-in-your-path/](https://disconnect3d.pl/2026/10/02/dont-put-tilde-in-your-path/) I was playing with the[nono](https://nono.sh/)agent sandboxing tool and it greeted me with a warning:`PATH entries the sandbox can write to: ~/\.local/bin/`which looked suspicious\. ![nono warning about PATH entries the sandbox can write to](https://disconnect3d.pl/assets/posts/tilde-in-path-nono-warning.png) In other words, doing this in your`~/\.bashrc`or`~/\.zshrc`: ``` export PATH="$PATH:~/.local/bin/" ``` will not expand the`~`\(tilde\) into the home path \(or`$HOME`\) as the tilde to home expansion happens only in unquoted inputs, as also the[bash documentation says](https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html): > If a word begins with an unquoted tilde character \(‘~’\), all of the characters up to the first unquoted slash \(…\) are considered a tilde\-prefix\. \(…\) Bash checks each variable assignment for unquoted tilde\-prefixes immediately following a ‘:’ or the first ‘=’, and performs tilde expansion in these cases\. \(…\) So instead of having`/home/<user\>/\.local/bin/`added to`PATH`we end up with`\./~/\.local/bin/`added to`PATH`\. And to fix this, we can do this: ``` export PATH="$PATH:$HOME/.local/bin/" ``` Note that the unquoted version`export PATH=$PATH:~/\.local/bin`actually works in Bash and Zsh, because tilde expansion is also performed in variable assignments after`=`and after each`:`\. But relying on that is fragile as for example, a whitespace will break the variable assignment\. The problem can also be seen here: ``` $ ls -la total 0 drwxr-xr-x@ 2 dc staff 64 Oct 2 13:37 . drwxr-x---+ 105 dc staff 3360 Oct 2 13:37 .. $ mkdir -p ./~/.local/bin/ $ printf '#include <stdio.h>\nint main() { puts("hello"); }'>a.c; gcc a.c -o ./~/.local/bin/kek $ PATH="~/.local/bin/" kek hello $ tree -f . ├── ./~ │ └── ./~/.local │ └── ./~/.local/bin │ └── ./~/.local/bin/kek └── ./a.c 4 directories, 2 files ``` ![Demo showing that a literal tilde in PATH resolves to a ./~/ directory in the current working directory](https://disconnect3d.pl/assets/posts/tilde-in-path.png) As we can see, the`kek`binary was found and executed from`\./~/\.local/bin/`— the home directory was never involved\. ## Check your PATH You can quickly check whether you have this problem with: ``` $ echo "$PATH" | grep -- '~' ``` or, to see each entry on its own line: ``` $ echo "$PATH" | tr ':' '\n' | grep '~' ~/.local/bin/ ``` If it prints anything, go fix your`\.bashrc`/`\.zshrc`/`\.profile`and replace the`~`with`$HOME`:\)\. Btw, kudos to the nono tool for warning about this \- even though the warning could be more verbose \(PR incoming\)\.

Similar Articles

A tale of two path separators (2021)

Lobsters Hottest

Explains how macOS handles two path separators (slashes and colons) due to its dual heritage from classic Mac OS and NeXTSTEP, and how this leads to files appearing with slashes in Finder but colons in the terminal.

@PierceZhang34: Practical Guide to SSH Tunnels. Although I use SSH tunnels daily, it always takes a while to recall the correct command. Should it be a local or remote tunnel? What parameters are needed? Is the port order local:remote or the reverse? Through a series of experiments, the author created a visual cheat sheet. Link: http:…

X AI KOLs Timeline

This tutorial provides a visual cheat sheet and practical labs for SSH local and remote port forwarding, covering common use cases like accessing private databases and exposing local servers.