Spaces behaviour in shell

Hello,

I am a bit puzzled by the way my shell treats spaces in filenames.

An example will be way clearer than any explanation I can make:

$ ls test\ file\ with\ spaces
test file with spaces
$ var="test\ file\ with\ spaces"
$ echo $var
test\ file\ with\ spaces
$ ls $var
ls: cannot access test\: No such file or directory
ls: cannot access file\: No such file or directory
ls: cannot access with\: No such file or directory
ls: cannot access spaces: No such file or directory

The problem is the same if I am using double quotes around the filename or when I nest a command displaying the filename.

I've been crawling on the internet for a long time, but I haven't been able to find any documentation on this particular issue.

Quote the variable with double quotes:

var="test file with spaces"
echo "$var"

Regards

No, I also tried that, but it didn't make it !

If the spaces are escaped with backslashes, AND you put the name between quotes, your shell is going to think the backslashes are part of the filename. The problem resides lower, that's why you need to use xargs, but once you know that, you can just do anything ! I actually used an input from a file instead of a variable (my command was actually something like xargs md5sum < filelist > sumlist).

Maybe I'm missing something but this is an example of my output:

$ ls -l "a b c"
-rw-r--r-- 1 user user 908 2008-11-24 21:02 a b c
$ zz="a b c"
$ ls -l "$zz"
-rw-r--r-- 1 user user 908 2008-11-24 21:02 a b c
$

Regards

Ah, sorry, I actually didn't try that because my filenames also had other special characters that I needed to protect (typically the single and double-quotes can be messy, with sed for example), but I'll remember that it works for simple cases !

Thanks !