32

The bash autocomplete feature does not seem to work with environment variables in 11.04. The current behavior is indicated below

export SCRIPT=/home/user/script
cd $SCRIPT/<tab>

results in a backslash (\) being added before $SCRIPT i.e. the prompt becomes

cd \$SCRIPT/

Same thing happens if cd is substituted with ls or any other command

Also, if there is an executable file in the path contained in $SCRIPT and I want to run that

$SCRIPT/<tab>

Bash does not show the options inside the folder (regardless of whether there is a single file or multiple files/directories inside the path contained in the environment variables).

In other words, autocomplete does not work with environment variables.

Enlico
  • 272
  • 1
  • 5
  • 15
nilchat
  • 329
  • 1
  • 3
  • 3
  • 2
    Here's the [bug in launchpad](https://bugs.launchpad.net/ubuntu/+source/bash/+bug/778627). – idbrii May 10 '11 at 23:26
  • I think it does not have to do with the bash version (4.1 or 4.2). I've just installed bash-4.1 from sources and used it instead of the 4.2.24 (Ubuntu 12.04's version; installed from sources so i don't have to 'hold back' the package at each sys upgrade) and the behaviour is exactly the same. Very, very annoying... –  Jul 30 '12 at 19:41
  • 4
    This question appears to be off-topic because it is about a bug in an old unsupported release. The bug is described [here](https://bugs.launchpad.net/ubuntu/+source/bash/+bug/778627) and has been fixed in 12.04 and later – Warren Hill Jul 11 '13 at 07:38
  • 1
    @WarrenHill This may be off-topic because it's about a bug (and I've voted to close it for that reason). That the bug is in an unsupported release is irrelevant though--we don't close old questions *that have answers* as off-topic when the affected release's EoL date comes along. Only new/unanswered EoL questions are off-topic. – Eliah Kagan Jul 11 '13 at 21:41

4 Answers4

18

To get the old behaivior back, use the command

shopt -s direxpand

or include it in your .bashrc

If you use the same .bashrc with different versions of bash, use

if ((BASH_VERSINFO[0] >= 4)) && ((BASH_VERSINFO[1] >= 2))
    then shopt -s direxpand
fi
user164395
  • 281
  • 2
  • 3
17

This bug was introduced in bash 4.2. There's a lengthy thread about it here:

http://lists.gnu.org/archive/html/bug-bash/2011-02/msg00274.html

In short, Chet Ramey, the developer of bash, isn't sure how to go about fixing it yet.

geirha
  • 45,233
  • 13
  • 70
  • 67
8

The workaround suggested in http://lists.gnu.org/archive/html/bug-bash/2011-02/msg00274.html is:

  1. use <Tab>to auto-complete your environment variable s.t. you command line says cd $MYVAR
  2. hit <Esc>+<Ctrl>-E to expand the current command line i.e. substitute $MYVAR by its value, the path
  3. add a / and then enjoy <Tab> auto-completion as usual

This assumes you are in emacs mode (set -o emacs) and have bash_completion set up sensibly for cd (e.g. complete -o nospace -F _cd cd).

Unfortunately this doesn't work in vi mode (set -o vi) because command line expansion is not available then.

kynan
  • 2,207
  • 1
  • 22
  • 24
2

After typing the shell variable

cd $SCRIPT/

type Cntrl-Alt-E. This expands the variable

cd /home/user/script/

This does work with EDITOR=vi in Fedora 16.