Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script that I want to accept any number of command line arguments.

So far I have

if [ -O $1 ] ; then
  echo "you are the owner of $1"
  echo "you are not the owner of $1"

Obviously if I wanted the script to only accept one argument this would work but what would work for ANY number of arguments.

ex.  ./script f f1 f2 f3
share|improve this question
If something answered your question, can you mark it as the answer (click the checkmark)? Thanks. – jtbandes Oct 8 '09 at 0:51

One possible way to do what you want involves $@, which is an array of all the arguments passed in.

for item in "$@"; do
  if [ -O "$item" ]; then
    echo "you are the owner of $item"
    echo "you are not the owner of $item"
share|improve this answer
Thanks this worked. – roger34 Oct 7 '09 at 23:59
Um, see what happens when you run script "arg with spaces". It may or may not be relevant to your situation. Once you're done, put doublequotes around $@ and $item. – wfaulk Oct 8 '09 at 0:15
Good point. Fixed. – jtbandes Oct 8 '09 at 0:27
Good answer!! +1 – lyarwood Oct 8 '09 at 1:00

Well, it depends on exactly what you want to do, but look into $*, $@, and shift.

share|improve this answer

"$@" does not solve his problem of "ANY number of arguments". there is a limit in how long a commandline can be ( a better way to read in "unlimited arguments" is via STDIN:

prg_which_creates_arguments | while read a; do \
    echo "do something with $a"; \

just create the arguments and pipe them one after another at the code which is doing something with them.

share|improve this answer

You must log in to answer this question.