Why does ~username/ change to /home/username if tab-completing a path with a space in it?

3

In ~jacksonc/temp/ there are two files, test.txt and space test.txt, both placed there through Windows (using Samba).

Using bash, if I type

cat ~jacksonc/temp/t[tab]

, it autocompletes to

cat ~jacksonc/temp/test.txt

However, if I type

cat ~jacksonc/temp/s[tab]

it autocompletes to

cat /home/jacksonc/temp/space\ test.txt

My question isn't about the backslash, I know why that's there. I'm curious as to why the space causes the ~ to be expanded though?

Cam Jackson

Posted 2011-07-06T03:58:07.660

Reputation: 2 267

shrug it doesn't do the same thing for me (using bash 4.1.5 in Linux). It could just be a bug. – Flimzy – 2011-07-06T04:36:45.317

I doubt it. More likely it's a user setting, as Drav Sloan suggested. – Cam Jackson – 2011-07-06T05:11:31.573

Just to provide another data point, using the default settings on current Ubuntu, bash expands ~ to the actual path when using tab completion regardless of whether there's a space in the path or not. I have no idea why the space would make a difference either; I've never heard of such a thing before. – Dave Sherohman – 2011-07-06T08:11:06.677

Mmm. Unfortunately, I can't give too many details on the system I'm using, as my work uses its own internal Linux distribution, though I believe it's essentially re-badged Redhat from like, 6 years ago. – Cam Jackson – 2011-07-06T23:27:19.727

One extra thing though, is that ~ always expands upon tab-completion when in ksh, which is the default shell. But if I fire up bash it only happens with a space in the path. Weird. – Cam Jackson – 2011-07-06T23:34:14.213

Here on ubuntu 10.04, with bash_completion installed ~ with a space in the path does not expand. It is a bit of a weird one though! – Drav Sloan – 2011-07-09T13:00:24.167

Answers

4

I would imagine because your system has bash_completion script http://bash-completion.alioth.debian.org/ installed, which can alter the behaviour of things like this.

Normally this expansion can be turned off via readline options. In your ~/.inputrc file put the following in it:

expand-tilde off

Drav Sloan

Posted 2011-07-06T03:58:07.660

Reputation: 396

Hmm, maybe. I wouldn't know, I'm SSH'd into a server somewhere in this building. Any idea, though, why this happens only when a space is in the path? Doesn't make much sense to me. – Cam Jackson – 2011-07-06T05:19:27.900

I just checked, and there's no ~/.inputrc file in my home directory, so it's not this. It must be something similar though, seeing as different people are getting different results with ~ being expanded. – Cam Jackson – 2011-07-08T06:01:10.500

Well it's more that the ~/inputrc can effect the behaviour of this expansion. I personally suspect bash_completion (a set of autocomplete routines compiled to expand the default autocomplete of bash; it's an addition to the stand bash install. Signs of that are a directory /etc/bash_completion.d/ or a file /etc/bash_completion. – Drav Sloan – 2011-07-09T12:59:03.667

Accepting this answer. I don't think it's correct in the exact thing causing the effect (ie bash_completion), but most likely it's some other script doing the same thing. So this is correct in principle :) – Cam Jackson – 2011-07-25T05:19:39.783