[Bash] Shift 命令

原创
2019/09/05 18:39
阅读数 276

shift

#This is a BASH shell builtin, to display your local syntax from the bash prompt type: help shift
-bash-4.1$ help shift
shift: shift [n]
    Shift positional parameters.

    Rename the positional parameters $N+1,$N+2 ... to $1,$2 ...  If N is
    not given, it is assumed to be 1.

    Exit Status:
    Returns success unless N is negative or greater than $#.

例子

#!/bin/bash
echo '>> before shift '
echo 'para count is ' $#
echo '$1 2 3 is ' $1, $2, $3.
shift 2
echo '>> after shift 2'
echo 'para count is ' $#
echo '$1 2 3 is ' $1, $2, $3.

输出:

-bash-4.1$ sh test a b c
>> before shift
para count is  3
$1 2 3 is  a, b, c.
>> after shift 2
para count is  1
$1 2 3 is  c, , .

shift可以用来向左移动位置参数。 Shell的名字 $0 第一个参数 $1 第二个参数 $2 第n个参数 $n 所有参数 $@ 或 $* 参数个数 $#

shift默认是shift 1 以下边为例:

-bash-4.1$ cat shift.sh
#!/bin/bash
until [ -z "$1" ]
do
  echo "$@"
  shift
done


-bash-4.1$ sh shift.sh 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9
4 5 6 7 8 9
5 6 7 8 9
6 7 8 9
7 8 9
8 9
9
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部