【Linux shell脚本的特殊变量】

4 篇文章 0 订阅

本节所介绍的特殊变量包括:"$0、$1、$2、$@、$*、$#、$$、$?”
直接上干货,后面有各特殊变量的解释:

[root@localhost script]# vim spevar.sh

[root@localhost script]# cat spevar.sh 
#!/bin/bash
echo "File Name: $0"
echo "First Parameter: $1"
echo "Second Parameter: $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters: $#"

[root@localhost script]# chmod +x spevar.sh 

[root@localhost script]# ./spevar.sh test1 test2
File Name: ./spevar.sh
First Parameter: test1
Second Parameter: test2
Quoted Values: test1 test2
Quoted Values: test1 test2
Total Number of Parameters: 2

$0:表示脚本文件的执行路径,大多数情况会列出脚本的名字
$1:表示传给脚本的第一个参数
$2:表示传给脚本的第二个参数
$@和$*:表示传给脚本的所有参数
$#:表示传给脚本参数的数量
chmod +x spevar.sh:给脚本加执行权限
./spevar.sh test1 test2:执行脚本,并给脚本传两个参数

[root@localhost script]# echo $$
2654
[root@localhost script]# echo $?
0
[root@localhost script]# LS
bash: LS: command not found...
Similar command is: 'ls'
[root@localhost script]# echo $?
127

$$: 表示当前shell的进程id
$?:返回上条命令的执行状态,返回结果为0,表示上条命令正确执行,返回结果非0,表示上条命令执行错误

快乐学习,不足之处,欢迎指出,创作不易,转载请注明出处,谢谢

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值