shell $*,$@,$#的区别详解
来源:互联网 发布:中小型网络组建技术 编辑:程序博客网 时间:2024/05/12 10:21
$*:如果加" ",即"$*"则表示视所有该命令参数为一个单词,如果没有" ",那么该命令每一个参数
都视为独立的参数。比如传入的参数:1 2 3 4,$*:表示1 2 3 4;"$*":表示1,2,3,4;
$@:不管有" "没有" ",每个参数都是独立的,但是,如果传入的参数比如是:1 "2 3" 4,那就有点差异了,"$@": 表示1,2 3,4;而$@:表示1,2,3,4;
$#:表示参数个数。
例子:
[yangzheng@localhost shell_test]$ cat test.sh
echo
index=1
echo "Listing args with\"\$*\":"
for arg in "$*"
do
echo "Arg #$index=$arg"
let "index+=1"
done
echo "All the parameters is considered to be a word."
echo "==========================================================="
echo
index=1
echo "Listing arg with\"\$@\":"
for arg in "$@"
do
echo "Arg #$index=$arg"
let "index+=1"
done
echo "All the parameters is considered to be separated words"
echo "==========================================================="
echo
index=1
echo "Listing arg with \$*:"
for arg in $*
do
echo "Arg #$index=$arg"
let "index+=1"
done
echo "All the parameters is considered to be separated words"
echo "==========================================================="
echo
index=1
echo "Listing arg with \$@:"
for arg in $@
do
echo "Arg #$index=$arg"
let "index+=1"
done
echo "All the parameters is considered to be separated words"
echo "==========================================================="
echo
index=1
echo "Listing arg with \$#:"
for arg in $#
do
echo "Arg #$index=$arg"
let "index+=1"
done
echo "echo params"
[yangzheng@localhost shell_test]$
[yangzheng@localhost shell_test]$ sh test.sh 1 2 3 4
Listing args with"$*":
Arg #1=1 2 3 4
All the parameters is considered to be a word.
===========================================================
Listing arg with"$@":
Arg #1=1
Arg #2=2
Arg #3=3
Arg #4=4
All the parameters is considered to be separated words
===========================================================
Listing arg with $*:
Arg #1=1
Arg #2=2
Arg #3=3
Arg #4=4
All the parameters is considered to be separated words
===========================================================
Listing arg with $@:
Arg #1=1
Arg #2=2
Arg #3=3
Arg #4=4
All the parameters is considered to be separated words
===========================================================
Listing arg with $#:
Arg #1=4
echo params
[yangzheng@localhost shell_test]$ sh test.sh 1 “?”
[yangzheng@localhost shell_test]$ sh test.sh 1 "2 3" 4
Listing args with"$*":
Arg #1=1 2 3 4
All the parameters is considered to be a word.
===========================================================
Listing arg with"$@":
Arg #1=1
Arg #2=2 3
Arg #3=4
All the parameters is considered to be separated words
===========================================================
Listing arg with $*:
Arg #1=1
Arg #2=2
Arg #3=3
Arg #4=4
All the parameters is considered to be separated words
===========================================================
Listing arg with $@:
Arg #1=1
Arg #2=2
Arg #3=3
Arg #4=4
All the parameters is considered to be separated words
===========================================================
Listing arg with $#:
Arg #1=3
echo params
[yangzheng@localhost shell_test]$
- shell $*,$@,$#的区别详解
- shell make的区别
- shell中“ “ ” ” ‘ “ ” ' “的区别
- shell中$*,$@,$# 的区别
- 【shell】test , [] , [[]] 的区别
- shell &> 与 >> 的区别
- 各种 shell 的区别
- shell之(),(()),``的区别
- shell ' ', " " 和 `` 的区别
- shell 中的$() ${} $[] []的区别
- shell和shell脚本的区别
- shell中$*与$@的区别
- shell,perl,python的区别
- shell、控制台、终端的区别
- shell,console,terminal的区别
- SHELL中&&与||的区别
- shell中$*与$@的区别
- shell中$*与$@的区别
- ArrayList-Linked-Set的循环中删除数据的错误显示
- OpenStack Hacker养成指南
- 函数lower_bound()返回值
- 如何让自定义布局失去焦点
- HDU 4952 Number Transformation 打表规律
- shell $*,$@,$#的区别详解
- 获取Android签名证书的sha1值,用于设置百度地图密钥
- ios java消息推送
- UITouch 触摸事件处理
- VLC-ANDROID
- dll破解方法之一
- 题集(汇总)
- 选择文件夹
- char a[]与 char *a