How to use cd with shell variable containing spaces

You can use wildcards:

export temp="/Users/username/Dropbox??Personal?"

or:

export temp="/Users/username/Dropbox*Personal*"

Now, cd $temp should work.


Besides the first assignment, all three work in assigning the correct value to $temp. But due to the way bash parses the command line, you need to write

cd "$temp"

afterwards to make it work (which also means that it is a good idea to always use "$var" in shell scripts).

It might be easier in your case to make an alias with

alias cdtemp='cd "/Users/username/Dropbox (Personal)"'

for use with cd.

For cp you still need to add the "" though. To avoid this, either name your folders without using characters with special meaning to the shell or create a symlink with ln -s ~/"Dropbox (Personal)" ~/DP.