18

I have a path "/third/party/city of las vegas"

when cd the path I use "/third/party/city of las vegas".

In .profile file I have exported the path to a variable as follows

export clv="/third/party/city of las vegas"

when I try to cd $clv it is throwing an error. How can I export a path which have spaces in the directory name

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Aravind
  • 1,559
  • 9
  • 31
  • 44
  • Please post the error, Try: `export clv=/third/party/city\ of\ las\ vegas` – Sepahrad Salour Jan 12 '15 at 15:05
  • If you visit that dir often, typing `cd "$clv"` is fairly long. You can reduce the time by making an alias instead: `alias goclv='cd /third/party/city\ of\ las\ vegas'` and then just type `goclv` every time you want to go to that dir. – LoMaPh Feb 14 '19 at 22:06

1 Answers1

24
export clv="/third/party/city of las vegas"

Is the same as

export clv=/third/party/city\ of\ las\ vegas

Either way, you still need to quote the variable.

cd "$clv"

The shell will break unquoted expansions on whitespace by default. Remembering to quote variables in contexts like this is a more conventional and probably safer practice.

Note that "one\ two" (trying to place an escaped space in quotes) will treat the \ literally.

mikeserv
  • 57,448
  • 9
  • 113
  • 229
goldilocks
  • 86,451
  • 30
  • 200
  • 258