Linux shell 脚本中, $@ 和$# 分别是什么意思?

如题所述

直接看示例:
[root@localhost xly]# cat t.sh
#!/bin/bash
echo $#
echo $@
[root@localhost xly]# sh t.sh
0
[root@localhost xly]# sh t.sh a b c
3
a b c
说明:
$@表示所有参数
$#表示所有参数的个数
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-05-08

$@:表示所有脚本参数的内容

$#:表示返回所有脚本参数的个数。


示例:编写如下shell脚本,保存为test.sh

#!/bin/sh

echo "number:$#"

echo "argume:$@"

执行脚本:

./test.sh first_arg second_arg

说明:给脚本提供了两个参数,所以$#输出的结果是2,$@代表了参数的内容!

第2个回答  2018-05-22

Linux shell 脚本中, $@ 和$# 分别是:

$@:表示所有脚本参数的内容

$#:表示返回所有脚本参数的个数。

示例:编写如下shell脚本,保存为test.sh

#!/bin/sh

echo "number:$#"

echo "argume:$@"

执行脚本:

./test.sh first_arg second_arg

说明:给脚本提供了两个参数,所以$#输出的结果是2,$@代表了参数的内容!

本回答被网友采纳
第3个回答  2012-04-19
$#,表示参数个数
$@,所有参数,并且所有参数都是独立的

例如 command a b c d
$#=4
$@="a" "b" "c" "d"
$@可以用来做 for each in本回答被提问者采纳
第4个回答  2012-04-19
$@表示所有参数
$#表示参数的个数
相似回答