In bash I set a variable like: o='OneDrive - MyCompany'. Then I use itby typing: cd "$o"/Docu. Then I press TAB to do word completion on the folder name. The result is cd $o/Documents. I hit enter and it fails because it stole my double quotes. HUH!?! I want to use "$o" as a very fast shortcut for that directory name. I don't want to type "$o/Docu and then press TAB and then ", which works. When I start typing I don't want to think about whether I might press TAB to expand something later in the path. I just want to follow the rule to double quote variables, but that doesn't work here.

I really want to be able to put backslashes before the spaces when defining the variable and have them be respected after expanding it. Is there any way I can do that? It would solve this problem so nicely.

What is the best way to deal with this? I don't want to fight the system, but I do want something that feels fast and natural.

Are there any shell settings that I should change to get closer to the expected behavior? I used tcsh for 25 years and am just now switching to bash. There's a lot to learn and tweak. My current shell options are:

$ shopt
autocd          off
cdable_vars     off
cdspell         off
checkhash       off
checkjobs       on
checkwinsize    off
cmdhist         on
compat31        off
compat32        off
compat40        off
compat41        off
compat42        off
compat43        off
completion_strip_exe    off
complete_fullquote      on
direxpand       off
dirspell        off
dotglob         on
execfail        off
expand_aliases  on
extdebug        off
extglob         off
extquote        on
failglob        off
force_fignore   on
globasciiranges off
globstar        on
gnu_errfmt      off
histappend      on
histreedit      on
histverify      on
hostcomplete    on
huponexit       off
inherit_errexit off
interactive_comments    on
lastpipe        off
lithist         off
login_shell     on
mailwarn        off
no_empty_cmd_completion off
nocaseglob      off
nocasematch     off
nullglob        off
progcomp        on
promptvars      on
restricted_shell        off
shift_verbose   off
sourcepath      on
xpg_echo        off

Note: This question is different from Why can't I use tab expansion/auto-complete of shell variables with cd? but turns out it has the same answer.

1 Answer 1


I just found the answer in Why can't I use tab expansion/auto-complete of shell variables with cd? I need to set shopt -s direxpand. This expands the variable and adds the coveted backslashes to escape the spaces even if I didn't use "$o". This is just what I need.

You must log in to answer this question.

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