9

Using Ubuntu 12.04. When I'm typing a path in terminal that starts with an env variable reference, I hit tab to complete a word and it escapes the $ in the env variable reference. As a result, I have to keep on going back to the beginning of the line and removing the backslash so I can continue navigating down a tree.

E.G.

Before Tab: user@test:~$ $HOME/Docum

After Tab: user@test:~$ \$HOME/Documents/

My $HOME/.bashrc has the "bash_completion" stuff enabled.

ruffEdgz
  • 2,112
  • 15
  • 14
David M. Karr
  • 1,306
  • 5
  • 18
  • 26
  • It seems like you're trying to report a bug. Please read [these instructions](https://help.ubuntu.com/community/ReportingBugs) carefully--they explain how to do this. If your intention is not to report this as a bug but to ask for workarounds to the problem, please clarify that by editing your question (but I still highly recommend reporting it as a bug because that might ultimately be the only way to get the problem fixed). – Eliah Kagan May 23 '12 at 21:16
  • Same as http://askubuntu.com/questions/41891/bash-auto-complete-for-environment-variables/ – geirha May 23 '12 at 21:49
  • I've been around long enough to know that sometimes "it just works like that" and that there might be a way to make it work differently. My instinct tells me that this isn't just a bug. – David M. Karr May 23 '12 at 22:05
  • This isn't really an answer, but it seems like a lot of extra typing to refer to `$HOME`. Why not use `~/Docum` instead? – Scott Severance May 24 '12 at 07:59
  • Also, for what it's worth, this isn't "just the way it works." I can't reproduce your issue. – Scott Severance May 24 '12 at 08:01
  • Concerning the "$HOME" example, it's because my actual test case wasn't using "$HOME", but a different var. I guess I'll report a bug for this, then. – David M. Karr May 24 '12 at 17:43
  • I've filed a bug report: https://bugs.launchpad.net/ubuntu/+source/bash/+bug/1004112 – David M. Karr May 25 '12 at 15:39

2 Answers2

2

It's something with the bash version that comes with Ubuntu 12.04.

I was able to compile bash from source to get my bash version up to 4.2.37, which resolved the issue.

user96328
  • 31
  • 3
  • I see the same *annoying* thing in CentOS7, bash version 4.2.46. – karsten Dec 18 '18 at 14:05
  • 12
    **Solution is to set "shopt -s direxpand"** see [here](http://www.usn-it.de/index.php/2017/01/21/oracle-enterprise-linux-7-how-to-stop-bash-tab-completion-from-escaping-the-dollar/) – karsten Dec 18 '18 at 14:12
  • 1
    @karsten THANK YOU for adding this comment even after it was closed! This still shows up as the top Google result for "bash tab adds backslash before dollar sign" now in 2022, and your comment is probably the reason why. I wish this question could be reopened and your comment added as an answer so you could get proper credit! – Ken Bellows Apr 20 '22 at 13:16
  • @Ken: You are very welcome, nice that it is useful for others. – karsten Apr 22 '22 at 08:44
1

You mention in the comments that your actual problem wasn't with '$HOME'. That's quite important since it matters if it is an environment variable or not.

Bash will complete possible environment variables first if you started with a dollar sign, finally followed by filename completion. So if it couldn't find a variable to complete for you, it could have found a filename, in which case it will escape the dollar sign with a backslash since that's a special character.

If you started your command with a program followed by some variables, completion could be handed over to programmable completion, which could also cause this. For example, typing gzip $HOME/ followed by tab will insert a backslash. The files handling programmable completion are owned by the package 'bash-completion', so either you can uninstall that package (I don't have in installed), or you can convince them it's a bug. Maintainers: http://bash-completion.alioth.debian.org/

Update: on Arch Linux, I tried out bash-completion version 2.0 and that works as expected for ls $HO<TAB>, but not for ls $HOME/<TAB> so I guess they still not solved this bug (Ubuntu uses version 1.3). You can try manually installing version 2.0, but note that it doesn't work completely. (Since the bug report also mentions it could be bash, I'm using version 4.2.37 instead of 4.2.24)

steabert
  • 1,776
  • 10
  • 12