The variable you want is indeed $@
- this contains all command-line arguments as separate words, each passed on intact (no expansion). ($*
treats all of them as a single word - good luck sorting it out if you have spaces in filenames).
You can loop, if you like. This is easily expanded to more complex actions than ls
.
for file in "$@"; do
if [ -f "$file" ]; then
ls -l "$file"
fi
done
Note: you should quote $@
to protect any special characters inside! You should also quote $file
for the same reason - especially inside the test. If there is an empty string in $@
, file
will also be empty, and without quotes, -f will attempt to act on the ']'. Errors ensue.
Also, if all you need to do is ls
(skipping your if
) you can just do this:
ls -l "$@"