转载:Shell 脚本必学技能:$1 变量详解
# 特殊变量说明
Shell 脚本中常用的特殊变量及其含义
变量 | 含义 |
---|---|
$0 | 脚本名称 |
$1 | 第一个参数 |
$2 | 第二个参数 |
$# | 参数总数 |
$@ | 所有参数列表 |
# 实用案例
通过实际案例学习命令行参数的使用
goto - 快速切换目录并查看内容
1 goto() { cd $1 && ls -al }
mkcd - 创建并进入新目录
1 mkcd() { mkdir -p $1 && cd $1 }
tq - 一键查询任意城市天气
1 tq() { curl wttr.in/$1 }
u - 快速 Git 提交并推送
1 u() { local msg=${1:-update} git add . && git commit -m "$msg" && git push }
reminder - 设置定时提醒
1 reminder() { (sleep $1 && osascript -e "display notification \"$2\" with title \"提醒\"") & }