4

I have a foldername with a space between the name: /Volumes/Img/Horos Data/DATABASE/

I need to put it in a variable to use in a script.

In terminal I can use \ to use the folder: cd /Volumes/Dados/Horos\ Data/DATABASE/, but it doesn't work when I put in a variable like:

DESTINY=/Volumes/Dados/Horos\ Data/DATABASE.noindex/
ls $DESTINY

How can I put it in a variable?

4

Variables or arguments on the right side of a command are subject to word spliting by the shell unless quoted. You can try the following demonstration.

touch foo\ bar\ baz
#
ls foo\ bar\ baz
foo bar baz
# or
ls "foo bar baz"
foo bar baz
#
var=foo\ bar\ baz
ls $var
ls: bar: No such file or directory
ls: baz: No such file or directory
ls: foo: No such file or directory
#
ls "$var"
foo bar baz
4

Quotes can be used to escape spaces when assigning to a variable.

var="abc def ghi"
  • It is not working with Quotes! I´ve tried with double and single quotes and return a error. DESTINY=/Volumes/Dados/Horos Data/DATABASE/ error= link_stat "/Volumes/Dados/Horos" failed: No such file or directory (2) – Vilefort Jul 5 '17 at 20:34
  • ... except it's when you use the variable via something like blah $var that the POSIX word split bites you – thrig Jul 5 '17 at 21:33

You must log in to answer this question.

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