Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 08-10-2014, 03:45 AM   #1
Senior Member
Registered: May 2007
Posts: 1,367

Rep: Reputation: 51
Question Stop bash from replacing "~" with "/home/username" when TAB is pressed

Hello, I've been using Bash for many years, but recently an unusual behavior started happening too much, so I'm trying to get rid of it where it shows.

When we start typing a command, for example: "vim ~/a.od" and before ending the filename we hit TAB, so the shell completes that name for us, I expect it to and write "vim ~/a.odt ". But at some machines I get a horrible and way too long "vim /home/way/to/long/path/to/username/a.odt " as expansion. Even if there are more options, like two files "a.odt" and "a.ods" the tilde path is expanded on the command line.

[1] I want the expansion to happen when before args be passed to the programs, but without cluttering the command line.

"bind -v" / "~/.inputrc" are ignored for this ( bash's bug, maybe!? ).

And I found this thread, but it doesn't answer [1], which is important to happen!

What do you think? How to fix this? And the problem might be on the system /etc/bash_completion (or similar files); so how do we fix it without losing the rest of the benefits this/these files add?

Last edited by dedec0; 08-10-2014 at 03:45 AM. Reason: typo
Old 08-10-2014, 05:25 AM   #2
LQ Guru
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,465

Rep: Reputation: Disabled
I can't replicate that behaviour. Using the tab to autocomplete a command leaves the "~" form of the home directory intact. The line below was created using the autocomplete feature:
~$ nano ~/.profile
The output of my BASH_VERSION is as follows:
What is yours?
The autocomplete also works properly on the tty's.
Old 08-10-2014, 06:29 AM   #3
Senior Member
Registered: May 2007
Posts: 1,367

Original Poster
Rep: Reputation: 51

The issue doesn't seem to be related to Bash version. And for distribution files (/etc/bash*), I have seen the issue on Ubuntu and a few others (not necessarily "Debianeses").

A few versions that have the problem right now, different distros:

GNU bash, version 3.2.48(1)-release (i486-pc-linux-gnu)
GNU bash, version 4.2.25(1)-release (i686-pc-linux-gnu)
GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)

And the issue does not happen for all commands. For example (and "[xxx]" is a keypress here!) :

# This expands:
vim ~/t[TAB]

# This doesn't:
alias vviimm=vim[ENTER] vviimm ~/t[TAB]
Want me to paste the /etc/bash_completion file (which I mostly don't understand) ? And maybe .bashrc and /etc/bashrc?

But trying to get rid of this I just did a "silly change" to ~/.bashrc to delete the _expand() function:

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion

## 1. Should this properly fix it? Is it enough?
## 2. Any side effects? I don't know!
## 3. Should "__expand_tilde_by_ref()" be erased too?
function _expand() { return 0; }
Seems to work, but I wanted to be sure of what to do, instead of magical recipes of trial and error, undocumented effects¹. :-/

¹ "undocumented" because the _expand* functions aren't clear for me to understand or don't have enough comments to do so.

Last edited by dedec0; 08-10-2014 at 06:38 AM. Reason: more words... LOL
Old 06-04-2015, 02:24 PM   #4
Senior Member
Registered: May 2007
Posts: 1,367

Original Poster
Rep: Reputation: 51
Angry Solution

I have found the exact cause of the related problem, and a solution.

If you could not reproduce, it may be because not all distros should have the same setup of configuration files. In my case, it is an Ubuntu machine. And I have seen it with Debian too (maybe it's common to many Debian derivatives).

The culprit: /etc/bash_completion

The culprit lines (already fixed, by commenting them and adding a "return" to leave no errors) :

# This function expands tildes in pathnames

    #   # FIXME: Why was this here?
    #   #[ "$cur" != "${cur%\\}" ] && cur="$cur\\"
    #   # expand ~username type directory specifications
    #   if [[ "$cur" == \~*/* ]]; then
    #           eval cur=$cur
    #   elif [[ "$cur" == \~* ]]; then
    #           cur=${cur#\~}
    #           COMPREPLY=( $( compgen -P '~' -u $cur ) )
    #           return ${#COMPREPLY[@]}
    #   fi
I don't know if the empty return could have some bad side effect, since it should be returning something. But it has been a few weeks, and I have not noted anything wrong.

It took me a lot of time, but this can be useful to someone else too.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script error!!! go to "usernameftp=$username"ftp" slufoot80 Linux - Server 6 08-27-2012 04:51 PM
How to stop bash from replacing "~/" with "/home/username"? daihard Linux - Software 8 08-22-2010 03:32 PM
Opera often hanging until "stop" pressed Wombat Pete Linux - Software 4 04-30-2010 09:55 AM
Why "bash-2.05b$" in Terminal instead of "username@localhost username" jayelitumbiolo Linux - General 9 10-04-2009 04:16 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:36 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration