To add on to what
@davids877 said: you may have noticed that whitespace matters in bash commands because the commands use whitespace to separate their arguments.
For example, suppose we've got a file with whitespace in it (Windows-style) called
My File.txt
. We store the filename in a variable x:
That's all well and good. But say we want to create that file now. What happens when we do
touch $x
?
Bash:
touch $x
ls
#> File.txt My
See how it has taken the whitespace in the filename and treated it as if it were whitespace in the command? So in
touch
's case, it treats it as two arguments, and creates two separate files.
To fix this, we put quotes around the variable recall, i.e.
"$x"
:
Bash:
touch "$x"
ls
#> File.txt My 'My File.txt'
Now it treats the entire variable x (whitespace and all) as a single filename, and we get our
My File.txt
file created exactly as we want.