How do I restore tab-completion on shell variables on the bash command-line?

5

1

I've long set my most-recently visited directories to shell variables d1, d2, etc.

On an ancient Fedora machine I could type a command like

$ cp $d1/

and the shell would replace $d1 with text like /home/acctname/projects/blog/ and would then show me the contents of .../blog, like you would expect any tab-completion to do.

Now, both ubuntu wheezy/sid and fedora 16 just \-escape the '$', and naturally there are no completions to show.

You can see this behavior in action in an OSX Terminal window. On 10.8, do something like

ls $HOME/ to see what I mean.

Is there a bash shell variable or option that can restore the old behavior?

man bash suggests this is a bug:

   complete (TAB)
          Attempt  to  perform  completion  on  the  text  before  point.  Bash
          attempts completion treating the text as  a  variable  (if  the  text
          begins  with  $),  username (if the text begins with ~), hostname (if
          the text begins with @), or command (including aliases and functions)
          in  turn.   If none of these produces a match, filename completion is
          attempted.

I get the above described completion when a token starts with '~' or a letter. It's just '$'-completion that's broken.

Eric

Posted 2012-10-24T21:56:14.923

Reputation: 421

Answers

9

As of bash 4.2, this behavior is governed by

shopt -s direxpand # enable
shopt -u direxpand # disable

See http://www.mail-archive.com/bug-bash@gnu.org/msg11251.html for background information.

jlf

Posted 2012-10-24T21:56:14.923

Reputation: 206

@Eric - you don't. If you have stuff that needs formatting that is relevant to your question, add it to the question instead. – ghoti – 2017-06-01T15:39:12.143

I really wanted to check this off, but I got this result:

ericp@pacer:apps $ shopt -s direxpand bash: shopt: direxpand: invalid shell option name

ericp@pacer:client $ bash --version GNU bash, version 4.2.24(1)-release (x86_64-pc-linux-gnu)

ericp@pacer:client $ echo $BASHOPTS checkwinsize:cmdhist:expand_aliases:extglob:extquote: force_fignore:histappend:interactive_comments:progcomp: promptvars:sourcepath

This is on Ubuntu 12.04.1 LTS – Eric – 2012-10-26T16:16:05.527

1How do I add newlines in stackexchange comments? – Eric – 2012-10-26T16:16:28.163

1

Not quite sure if you are asking for a solution on your Mac or for *nix. If you are asking about changing your Mac, I just ran through this simple tutorial to upgrade my OSX bash from

GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15)
Copyright (C) 2007 Free Software Foundation, Inc.

to

GNU bash, version 4.3.42(1)-release (x86_64-apple-darwin15.0.0)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

David Newcomb

Posted 2012-10-24T21:56:14.923

Reputation: 111

Actually, it works the way I expected it to on both osx 10.1 and ubuntu 14.04 (jessie/sid): '$d<TAB>' expands all vars that start with '$d', and '$d1/<TAB>' shows the files in the dir referenced by '$d1'. Thanks all. – Eric – 2016-01-22T00:15:07.930