正确使用$ @

rog*_*r34 3 unix variables bash

我试图编写一个小脚本,接受任意数量的命令行参数,打印出rwx文件(不是目录)的权限

我拥有的是什么

file=$@    
if [ -f $file ] ; then    
ls -l $file    
fi
Run Code Online (Sandbox Code Playgroud)

但是,它只接受一个命令行参数.谢谢你的帮助.

Pau*_*ce. 8

以下是$*$@,有和没有引号之间的一些差异的演示:

#/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
Run Code Online (Sandbox Code Playgroud)

运行它:

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

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

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

"$@": ..abc..
"$@": ..space here..
Run Code Online (Sandbox Code Playgroud)