On Thu, Jan 23, 2025 at 5:31 PM Lawrence Velázquez <v...@larryv.me> wrote: > > On Thu, Jan 23, 2025, at 6:37 PM, Keith Thompson wrote: > > Description: > > A literal '~' or other tilde-prefix at the beginning of an > > element of $PATH is expanded when a command is executed > > from the bash prompt. This is undocumented and inconsistent with > > the behavior of other commands that can execute commands. > > > > This expansion doesn't happen when bash is invoked as "/bin/sh" > > (which is a symlink to bash on some systems). > > The texinfo documentation mentions it as something that is disabled > in POSIX mode: > > 19. Literal tildes that appear as the first character in > elements of the `PATH' variable are not expanded as > described above under Tilde Expansion. > > https://www.gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html
That explains why /bin/sh (which is what C's system() function uses, as in my sample code) doesn't exhibit the behavior, even if /bin/sh is a symlink to bash. But I don't see anything in the "Tilde Expansion" section that documents the behavior of a literal '~' in $PATH. I suggest it would be better to document the behavior, since I imagine it's fairly easy to run into it accidentally. For example, something like PATH="~/bin:$PATH" will trigger the behavior. > > There could be existing code that depends on it -- which I > > suppose argues against removing it at all. The documentation > > should at least mention that the expansion occurs and discuss > > this inconsistent treatment of $PATH by bash vs. other tools. > > Perhaps this is another $[...] situation -- keeping the feature > around for compatibility, but leaving it undocumented to discourage > users from learning about and using it. $[...] appears to behave like $((...)). Is it equivalent? It's less likely that someone could write that accidentally. But the fact that it's undocumented could cause problems for anyone who sees it in an existing script and needs to figure out what it does. Perhaps this was already debated, but my preference would be to document it with a note discouraging its use.