Store paths containing directory with space in the name

2

Personally I have the following problem: I need in my script to store a path of an X directory in a variable. The big problem is that one of the folders in this path contains space in the name and for some reason when calling the variable it gives a conflict in the directory with space. Ex:

If I use it this way:

cd "/home/douglas/Minha Pasta" 

But if so, it does not work:

home="/home/douglas/Minha Pasta"
cd $home

Does anyone know how I can resolve this?

    
asked by anonymous 27.10.2016 / 14:47

2 answers

3

Another option is to reference the variable in double quotation marks:

home="/home/douglas/Minha Pasta"
cd "$home"

According to documentation, 5.1 . Quoting Variables :

  

(In free translation)

     

When referring to a variable, it is often advisable to enclose its name in double quotation marks.

     

This prevents reinterpretation of all special characters   within the quoted string - except $ , ' (grave accent), and \   (exhaust). [...]

     

Use double quotes to avoid word separation. An argument   quotation marks appears as a single word, even if it contains   space separators.

    
27.10.2016 / 17:42
4

You will need to escape the spaces for the command to work, when you use the cd $home command it is the same as: /home/douglas/Minha Pasta , it will read up to My and will not find the paste;

Make home="/home/douglas/Minha\ Pasta" , with \ before space.

    
27.10.2016 / 17:20