I have an environment variable stored in my .bash_profile called $MODULES:


When I am at the bash prompt and I type cp $MOD and then press TAB to complete, how do I get it to expand to:

sudo cp /var/www/html/sites/all/modules/

...and then I can immediately continue on with a subfolder and tab-complete that as well? Right now, when I do TAB completion it resolves to:

sudo cp \$MODULES/

...which means it's looking for a directory in the current location named "$MODULES", which is obviously not what I want.

1 Answer 1


Set the relevant Bash shell option with shopt -s direxpand to expand directory variables to its full path using tab completion. Add that line to your .bashrc to make the option permanent.

Appending the variable with a forward slash is required before pressing tab.

  • This was previously the default behavior in Bash before 4.2. I believe there are still supported macOS versions out there with /bin/bashes this old, so you may wish to put shopt | grep -q '^direxpand\b' && shopt -s direxpand in your ~/.bashrc to account for both Bash < 4.2.29 and Bash ≥ 4.2.29, where direxpand was introduced. source Sep 24, 2022 at 22:51

You must log in to answer this question.

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