博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shell中$*,$@,$# 的区别
阅读量:4040 次
发布时间:2019-05-24

本文共 1353 字,大约阅读时间需要 4 分钟。

$@ 和  $* 只在被双引号包起来的时候才会有差异

双引号括起来的情况:
$*将所有的参数认为是一个字段
$@以 默认为空格 来划分字段,如果空格在“”里面,不划分
没有括起来的情况是$@和$*一样的,见到 空格 就划分字段

$#是 程序的 参数个数(不包括$0)

$? 获取上一次命令执行的返回值,一般 执行 成功 返回0。

$0 $1 $2 以此类推,取命令行参数,如 test.sh a b c ,则 $0 是 test,$1是 a, $2是b,$3是c。

一个小例子 ,仅供参考

test.sh内容如下:

#!/bin/bash

index=1
echo "Listing args with\"\$*\":"
for arg in "$*"
do
echo "Arg #$index=$arg"
let "index+=1"
done
echo "all parameter is one word"
echo
index=1
echo "Listing args with \"\$@\":"
for arg in "$@"
do
echo "Arg #$index=$arg"
let "index+=1"
done
echo "all parameter is all kinds of different words"
echo
index=1
echo "Listing args with \$* "
for arg in $*
do
echo "Arg #$index=$arg"
let "index+=1"
done
echo "all parameter is all kinds of different words"

echo

index=1
echo "Listing args with \$r@ "
for arg in $@
do
echo "Arg #$index=$arg"
let "index+=1"
done

echo

echo "all parameter is all kinds of different words"

运行结果如下

$ ./test.sh 1 2 3 "4 5"
Listing args with"$*":
Arg #1=1 2 3 4 5
all parameter is one word
Listing args with "$@":
Arg #1=1
Arg #2=2
Arg #3=3
Arg #4=4 5
all parameter is all kinds of different words
Listing args with $*
Arg #1=1
Arg #2=2
Arg #3=3
Arg #4=4
Arg #5=5
all parameter is all kinds of different words

Listing args with $r@
Arg #1=1
Arg #2=2
Arg #3=3
Arg #4=4
Arg #5=5
all parameter is all kinds of different words

the number of all parameter is 4

转载地址:http://zipdi.baihongyu.com/

你可能感兴趣的文章
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
补充自动屏蔽攻击ip
查看>>
谷歌走了
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
让我做你的下一行Code
查看>>
浅析:setsockopt()改善程序的健壮性
查看>>
关于对象赋值及返回临时对象过程中的构造与析构
查看>>
VS 2005 CRT函数的安全性增强版本
查看>>
SQL 多表联合查询
查看>>
Visual Studio 2010:C++0x新特性
查看>>
drwtsn32.exe和adplus.vbs进行dump文件抓取
查看>>
cppcheck c++静态代码检查
查看>>
在C++中使用Lua
查看>>