On a remote computer with bash version "3.2.51(1)-release" (OS: SUSE Linux Enterprise Server 11.1 (x86_64)), I could not do a usual tab completion with variable name. for example, I'd like to type:

echo $OLDPWD/

then at the end of the slash, I can hit "Tab" and the variable name would be expanded to the old directory name (before the last cd or pushd command). This works in my own laptop (Debian 7, Bash 4.2.37(1)-release). Why in this SLES bash shell it would not work?

Similarly, if I type

echo $OLDP

and then hit "Tab" at the end of the "P" there, bash would complete that to $OLDPWD. This would not happen in SLES 11 bash too.

What control variables (shopt? set?) that affect this behavior?

  • Does Alt+$ work?
    – demure
    Commented Jun 5, 2013 at 16:53
  • Probably the older bash version does not have all the fancy completion functions included. Can you install the bash-completion package on the remote machine? Commented Jun 5, 2013 at 20:06
  • @glennjackman: The bash-completion package provides a set of wrappers around many commands that customize the argument completion behavior for those commands. It does not affect the completion of environment variables. That capability is built into later versions of bash.
    – garyjohn
    Commented Jun 5, 2013 at 22:56
  • 2
    Is $OLDPWD set? In my bash 4.2.25 it is not defined until you've used cd at least once. I don't know if bash 3.x set $OLDPWD when you did cd. Does completion of, say, $HOM<tab> complete to $HOME? Commented Jun 6, 2013 at 2:07
  • by the way, I just found in csh, if I set set zzz='~/some/dir', I can not do tab expansion for cp $zzz +tab but If I define by set zzz=~/some/dir, I can do the expansion. Looks like the shell cannot expand after string in quotes.
    – Chan Kim
    Commented Mar 6, 2017 at 7:19

3 Answers 3


shopt -s direxpand will make echo $HOME/<tab> expand to echo /home/matt/ in bash 4.2. In bash 4.1 it should be the default.

I find that bash-completion can really mess things up. I always either uninstall bash-completion or run complete -r to make sure that the individual completion rules aren't doing something stupid to the default completions.

If $OLDPWD is actually set then echo $OLDP<tab> should expand to echo $OLDPWD. I don't know what would keep that from happening.

  • How do we do it in csh? I see 'shopt: Command not found.'.
    – Chan Kim
    Commented Mar 6, 2017 at 7:08

Try C-x $ instead of TAB to explicitly request possible variable (not something else) completions.


Check the answer for urxvt - tab expand environment variables by Dmitry Alexandrov.

"This is about direxpand option.

$ shopt -s direxpand and $FOO_PATH/

will be expanded by TAB"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .