Comment by tremon
4 years ago
And where it isn't needed. If you have a path that contains a variable and a space, bash will happily escape the $, making the path invalid. See the following:
$ cd $HOME
$ mkdir my\ dir
$ ls my[tab]
$ cd /
$ ls $HOME/my[tab]
ls: cannot access '$HOME/my dir/': No such file or directory
That error is because when you press [tab], bash changed the path to \$HOME/my\ dir/ but that isn't obvious from the output and I couldn't find a proper way to include the tab-expanded result in the transcript.
(edit: this is on GNU bash, version 4.3.48(1)-release but I've seen this behaviour for years)
Depends on the Bash version, I guess? Mine is 4.4.20(1) and when I do "cd $HOME/my[TAB]", it replaces the input line with "cd /home/joker/my\ dir/", and pressing [ENTER] changes the directory to '/home/joker/my dir', as can be seen from the prompt.
The variable escaping behavior has existed for a while https://stackoverflow.com/questions/32463052/bash-tabbing-fo... https://askubuntu.com/questions/70750/how-to-get-bash-to-sto... https://askubuntu.com/questions/41891/bash-auto-complete-for...
And I experience the problematic behavior on my Ubuntu VM. However, I can get the above describe expansion behavior if I run: shopt -s direxpand