46.7 Quoting and Command-Line ParametersQ: I need to pass a shell script some arguments with multiple words. I thought that putting quotes (8.14 ) around command-line arguments would group them. The shell script seems to ignore the quoting, somehow. Here's a simple example: Q: $ A:
This is the way A: $1 $2 A:
[not A: for arg in 1 2 3 4 A: Note that the quotes are gone. What you wanted the shell to see was: A: for arg in '1 2 3' 4 A: You cannot get that, but you can get something that is Good Enough: A:
A:
In effect, A: $1" "$2 A:
Putting A: for arg in "$1" "$2" A: Shell quoting is unnecessarily complex. The C shell actually has the right idea (variables can be set to "word lists" (47.5 ) ; argv is such a list), but its defaults and syntax for suppressing them make for an artless programming language: A: foreach arg ($argv:q) # colon q ?!? A:
For the special case of iterating a shell variable over the argument
list as it stands at the beginning of the iteration, the Bourne shell
provides the construct A: for arg do echo "Argument is $arg" done A: produces: A: Argument is 1 2 3 Argument is 4 A:
A: $1" "$2...$ A:
(where A: "$@" A: expands to: A: "" A:
and A: % A:
The construct - in comp.unix.questions on Usenet, 18 March 1988 |
|