shell脚本$0 $1 $2 $* $@ $# 变量学习记录

本文深入探讨了Bash脚本中变量与参数的使用方法,包括如何获取脚本名称、参数总数、所有参数及其独立和组合形式,以及如何处理特定数量的参数。通过实际案例展示了Bash脚本在参数处理上的灵活性。
#!/bin/bash
#this program is use $0 $# and so on
echo "THe script name is  ==> ${0}"
echo "The total parameter number is ==> $#"
["$#" -lt 2 ] && echo "the number of parameter is less than 2,Stop here ." && exit 0
echo "your whole parameter is '$@'"
echo "your whole para is '$*'"
echo "the 1st parameter ==>${1}"
echo "the 2dn parameter ==>${2}"
echo "the 2dn parameter ==>${3}"
echo "the 2dn parameter ==>${4}"

 

执行结果:

v2x@ubuntu:~/shell_script_learn$ ./bianliang.sh I love you 520          这边执行时有4个参数
THe script name is  ==> ./bianliang.sh
The total parameter number is ==> 4
./bianliang.sh: line 5: [4: command not found
your whole parameter is 'I love you 520'
your whole para is 'I love you 520'
the 1st parameter ==>I
the 2dn parameter ==>love
the 2dn parameter ==>you
the 2dn parameter ==>520
 

echo "THe script name is  ==> ${0}"  这个脚本的名字
echo "The total parameter number is ==> $#"         这个脚本执行时有几个参数
["$#" -lt 2 ] && echo "the number of parameter is less than 2,Stop here ." && exit 0
echo "your whole parameter is '$@'"        脚本执行时的所有参数,参数各自独立
echo "your whole para is '$*'"                   脚本执行时的所有参数,参数用一个空格隔开,是一个整体


echo "the 1st parameter ==>${1}"           脚本执行时的第一个参数
echo "the 2dn parameter ==>${2}"          脚本执行时的第二个参数
echo "the 2dn parameter ==>${3}"          脚本执行时的第三个参数
echo "the 2dn parameter ==>${4}"          脚本执行时的第四个参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aFakeProgramer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值