Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using $@ properly

I am trying to write a tiny script that accepts any number of command line arguments that prints out the rwx permissions for a file (not directory)

What I have is

file=$@    
if [ -f $file ] ; then    
ls -l $file    
fi

This accepts only one command line argument however. Thanks for any help.

like image 637
9 revs Avatar asked May 12 '26 14:05

9 revs


1 Answers

Here is a demonstration of the some of the differences between $* and $@, with and without quotes:

#/bin/bash
for i in $*; do
    echo "\$*: ..${i}.."
done; echo
for i in "$*"; do
    echo "\"\$*\": ..${i}.."
done; echo
for i in $@; do
    echo "\$@: ..${i}.."
done; echo
for i in "$@"; do
    echo "\"\$@\": ..${i}.."
done; echo

Running it:

user@host$ ./paramtest abc "space here"
$*: ..abc..
$*: ..space..
$*: ..here..

"$*": ..abc space here..

$@: ..abc..
$@: ..space..
$@: ..here..

"$@": ..abc..
"$@": ..space here..
like image 89
Dennis Williamson Avatar answered May 15 '26 06:05

Dennis Williamson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!