start page | rating of books | rating of authors | reviews | copyrights

Unix Power ToolsUnix Power ToolsSearch this book

27.5. Output Command-Line Arguments One by One

Figure Go to http://examples.oreilly.com/upt3 for more information on: showargs

When you're experimenting with shell quoting, it's nice to be able to see how arguments on a command line are quoted. Here's a demo of a simple bash script[83] named showargs; you might want to save it in a file and run it yourself (Section 35.1). The script shows how many arguments were passed to it. Then it lists the arguments, one per line, surrounded by >> << to show leading or trailing spaces.

[83]The script uses bash because, as this article explains later, its built-in echo (Section 27.5) command has the -E option to prevent interpretation of special characters.

cat Section 12.2, && Section 35.14, $# Section 35.20, path Section 35.7

% cat showargs
#!/bin/bash
test $# -ne 1 && s=s
echo "I got $# argument$s:"
for arg
do echo -E ">>$arg<<"
done
% showargs "Start of path:" $path[1-3] "  that's it!  "
I got 5 arguments:
>>Start of path:<<
>>/u/jpeek/bin<<
>>/bin<<
>>/usr/bin<<
>>  that's it!  <<

The output from your shell may differ from that shown above, which is the result of running showargs in tcsh. bash doesn't have a $path variable, for example. And zsh expects a comma, rather than a hyphen, to separate the range. But as long as the arguments to showargs are quoted properly, you should get the result you're looking for, with a little tweaking, of course!

--JP and SJC



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.