3

I am trying to create an environment variable for a path that is long, has a space, and syntax.

I tried the following:

export temp=/Users/username/Dropbox (Personal)

And added it to my .profile.

I also tried:

export temp='/Users/username/Dropbox (Personal)'
export temp="/Users/username/Dropbox (Personal)"
export temp=/Users/username/Dropbox\ \(Personal\)

None of the above work. My end goal is to type cd $temp or cp $temp.

2 Answers 2

2

You can use wildcards:

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

or:

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

Now, cd $temp should work.

0
12

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.

5
  • I'd considered "$temp" but it seems a little less simple than what I wanted. An alias command notwithstanding is there any alternative that would allow me to use $temp (without quotes"?
    – undulaten
    Dec 7, 2015 at 7:19
  • Use folder names without characters having special meaning for the shell (the space in your case). Or make a symlink with ln -s ~/"Dropbox (Personal)" ~/DP and use this afterwards.
    – nohillside
    Dec 7, 2015 at 7:23
  • 1
    cd "${temp}" is even better at making sure everything is correctly escaped.
    – orkoden
    Dec 7, 2015 at 10:54
  • 1
    @orkoden Using {} helps in separating the name of an variable from other text (which is not required here), it doesn't impact the interpretation of the content of the variable
    – nohillside
    Dec 7, 2015 at 11:44
  • It is a good point. Changing the folder to something without special characters is something I looked into. Dropbox doesn't seem to allow one to change the folder name (at this time).
    – undulaten
    Dec 7, 2015 at 15:34

You must log in to answer this question.

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