#1040820 bash-completion: ~ (tilde) should not be quoted if a file contains a $ (dollar)

#1040820#5
Date:
2023-07-11 05:30:39 UTC
From:
To:
Dear Maintainer,

Currently, if a file with '$' (dollar sign) in it exists, Bash
completion will quote a leading '~' (tilde) and not quote the tilde.

To reproduce (where '^I' represents hitting the Tab key):

    $ touch ~/'APL$FONT'

    $ echo ~/APL^I
   -> echo \~/APL$FONT

As you can see, the wrong symbol was quoted. Even worse, if the $ had
been quoted using '\' (backslash), the backslash will be removed.

    $ echo ~/APL\$FONT^I
   -> echo \~/APL$FONT

Note that using $HOME fails in the same ways. It does not add a
backslash and will remove one that the user types.

    $ echo $HOME/APL^I
   -> echo $HOME/APL$FONT

    $ echo $HOME/APL\$FONT^I
   -> echo $HOME/APL$FONT

However, replacing the ~ or $HOME with the full path does work as
it should. (Mostly. See below.)

    $ echo /home/ben/APL^I
   -> echo /home/ben/APL\$FONT

I believe the following is not relevant for this bug, but I include it
for completeness.
----------------------------------------------------------------------
Trying to use a single tick (') to quote the dollar sign does not work
and completion fails with no possibilities. However, that appears to
be a general lack in bash_completion and probably should be its own
bug.

    $ echo ~/'APL^I^I
   -> echo ~/'APL

    $ echo $HOME/'APL^I^I
   -> echo $HOME/'APL

    $ echo /home/ben/'APL^I^I
   -> echo /home/ben/'APL

Single tick does work if at the beginning of a fully specified
pathname, but of course that doesn't help for ~ or $HOME.

    $ echo '/home/ben/APL^I
   -> echo '/home/ben/APL$FONT'
---------------------------------------------------------------------- By the way, I was a bit confounded debugging this. I traced through all the functions in /usr/share/bash-completion/bash_completion and it seems like it is correctly detecting the leading '~' and makes an effort to avoid quoting it. I even tried `shopt -u progcomp` and was still experiencing the bug! The only way I could stop it was to disable tab completion completely in Bash using `bind "set disable-completion on"`. Could it be a bug in readline? Whatever it is, there is deeper magic going on here than I understand.