How to write a script to accept any number of command line arguments? (UNIX, BASH)

bashscriptunix

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"
else
  echo "you are not the owner of $1"
fi

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

Best Answer

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"
  else
    echo "you are not the owner of $item"
  fi
done
Related Question