深入理解Shell脚本:特殊变量、条件语句与循环结构
1. 特殊变量
在编写Shell脚本时,特殊变量是提升脚本灵活性的关键。以下是几种常见的特殊变量及其用途。
- 单个参数变量 : $1 、 $2 等正非零整数命名的变量,存储脚本的参数值。例如,创建一个名为 pshow 的脚本:
#!/bin/sh
echo First argument: $1
echo Third argument: $3
运行脚本 ./pshow one two three ,输出结果为:
First argument: one
Third argument: three
此外, shift 命令可用于移除第一个参数,并将后续参数依次前移。例如,创建 shiftex 脚本:
#!/bin/sh
echo Argument: $1
shift
echo Argument: $1
shift
echo Argument: $1
运行 ./shiftex one two three ,输出为:
Argument: one
Argument: two
Argument: t
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



