1

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.

2

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.

  • Tried it that way, didn't work either. I suspect something is wonky with my bash, because it works on Ubuntu. – G. Bach Nov 22 '14 at 1:36
  • I just tested in Yosemite and it's fine. – miken32 Nov 22 '14 at 1:40
  • The above script also works for me. @G.Bach Did you notice mike's comment about not using slashes and quotes at the same time when you call it. Do this command: ./script.sh "/Users/me/some directory with whitespace/" -- with quotes and no backslashes and in your script use "$1" not "$(1)" – beroe Nov 22 '14 at 2:58
  • I tried that as well, it doesn't work in my OS X, but does in Ubuntu - very strange. Thank you both for trying it out! – G. Bach Nov 22 '14 at 3:32
1

The correct script is

#!/bin/bash
echo "${1}"
ls "${1}"

(You can start with #!/bin/sh instead if you prefer.)

The syntax of bash and other shells is a bit weird. You might think that $var means “the value of var”, but it doesn't. It means “take the value of var, split it at each whitespace sequence (or more generally, split it according tot he value of IFS), and interpret each part as a glob pattern which, if it matches any file, is replaced by the list of matches”. Yes, that's quite a mouthful — which is why you should always write "$foo" instead. Always put double quotes around variable substitutions unless you know why you need to leave them out. Inside double quotes, the meaning of "$foo" is “the value of the variable foo”.

See Why does my shell script choke on whitespace or other special characters? for further discussion of this topic.

To invoke your script, you can use any form of quoting — but only one:

./script.sh /Users/me/some\ directoy\ with\ whitespace/
./script.sh "/Users/me/some directoy with whitespace/"
./script.sh '/Users/me/some directoy with whitespace/'

If ./script.sh /Users/me/some\ directoy\ with\ whitespace/ doesn't work with the quotes in the script, then you aren't typing that in a bash shell. I can't guess what else you might be typing it in. Or else you didn't test with the version of the script you thought you had.

  • I don't quite remember how I fixed my problem, but I think I tried the script you suggested - without success. I must be on a very strange bash configuration with that OSX since everything works as intended in other OSes. Thanks for the link! – G. Bach Dec 16 '14 at 14:51

You must log in to answer this question.

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