9

In the previous versions of Ubuntu 18.04, the variables in Bash are expanded when I press the Tab key. But in Ubuntu 20.04 (using bash 5.0.16), the variables are not expanded. Instead, the dollar sign $ before the variable gets proceeded by a backslash.

For example, let's say, I have a variable MY_DIRECTORY:

export MY_DIRECTORY=/path/to/a/folder

Now when I write something like this:

ls $MY_DIRECTORY<Tab key>

I get:

ls \$MY_DIRECTORY

As you can see, the variable doesn't expand to the desired path. What is wrong with that?

1 Answers1

17

You have the following two options for expanding a variable in Bash:

  • Use the Ctrl+Alt+E keyboard shortcut whenever you want to expand a variable.

    For example, if I write in my terminal:

    $LANG $BASH
    

    and then press the shortcut, the above will expand both variables to:

    en_US.UTF-8 /usr/bin/bash
    

    From man bash, if you search for "M-C-e" ("M" standing for "Meta", which is the Alt key in most keyboards and "C" standing for Control):

    shell-expand-line (M-C-e)
           Expand the line as the shell does.  This performs alias and  history  expansion  as
           well  as  all  of  the  shell  word  expansions.  See HISTORY EXPANSION below for a
           description of history expansion.
    
  • Enable the shopt builtin's direxpand option by running in your terminal:

    shopt -s direxpand
    

    Now, if you type:

    ls $MY_DIRECTORY/<Tab key>
    

    it will be expanded to:

    ls /path/to/a/folder/
    

    To have the direxpand option enabled for all terminal sessions, append shopt -s direxpand in your ~/.bashrc file either manually or by running:

    echo "shopt -s direxpand" >> ~/.bashrc