shift 命令用于对参数的移动(左移),一般用于在不知道传入参数个数的状况下依次遍历每一个参数而后进行相应处理(常见于 Linux 中各类程序的启动脚本)。shell
在扫描处理脚本程序的参数时,常常要用到的shift命令,若是你的脚本须要5个或5个以上的参数,你就须要用shift命令来访问第5个及其后面的参数。bash
做用:每执行一次,参数序列顺次左移一个位置,$#(传递到脚本的参数个数)的值减 1,用于分别处理每一个参数,移出去的参数,再也不可用.ide
[root@test shell]# cat shift.sh #!/bin/bash if [ $# -le 0 ];then echo "无可用参数" exit fi sum=0 while [ $# -gt 0 ] ;do sum=$[$sum+$1] shift done echo "total is $sum" [root@test shell]# sh shift.sh 1 2 3 total is 6 [root@test shell]#
我的公众号:
spa