Escaping variable in script I'm using script.sh in an OS X Mavericks terminal containing only the following:

echo ${1}
ls ${1}


Now, when I call ./script.sh "/Users/me/some\ directoy\ with\ whitespace/", the echo command prints /Users/me/some\ directoy\ with\ whitespace/, but ls still insists on splitting everything up, so the results there are

ls: /Users/me/some\: No such file or directory
ls: directoy\: No such file or directory
ls: with\: No such file or directory
ls: whitespace/: No such file or directory


Notice the \ at the end of the first three error lines. How do I do this correctly?

(I'm aware that using both "" and \ in the input argument leaves the \ in my variable, but I assume I would need those.)

I also tried using ./script.sh /Users/me/some\ directoy\ with\ whitespace/ without the quotes, then making the script

echo ${1}
ls "${1}"


but that made the echo output not contain any \ now, and the ls output looks like this:

ls: "/Users/me/some\: No such file or directory
ls: directoy\: No such file or directory
ls: with\: No such file or directory
ls: whitespace/": No such file or directory


Notice that all it did was add the quotes without seeming to understand them as syntax.



It is somehow related to the IFS (input field separator) since if I make the script

IFS=*
ls ${1}


and run it as ./script.sh /Users/me/some\ directoy\ with\ whitespace/, it correctly shows me the content of the specified directory. I'm not sure why, though.
You don't need the braces. This should work just fine:

echo $1
ls "$1"


You don't want to use backslashes and quotes in the directory name. One or the other should be fine.