[Bash] Shift 命令

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

转载于:https://my.oschina.net/u/553266/blog/3102529

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
bash编程中,shift是一种用于处理命令行参数的内置命令。当我们在脚本中使用shift命令时,它会将命令行参数列表中的参数向左移动一个位置。这意味着第一个参数被丢弃,第二个参数变成新的第一个参数,依此类推。 shift命令可以用于循环遍历命令行参数以及处理多个参数的情况。通过使用shift命令,我们可以逐个处理命令行参数,而无需手动对每个参数进行索引。 以下是一个使用shift命令处理命令行参数的示例代码: ``` #!/bin/bash while [ $# -gt 0 ] do echo "参数值: $1" shift done ``` 在上述示例中,我们使用了一个while循环来迭代处理命令行参数。`$#`表示命令行参数的数量,`$1`表示当前的第一个参数。在循环的每次迭代中,我们打印出当前的参数值,并使用shift命令将参数列表向左移动一个位置。 这样,当我们在命令行中执行脚本并传递一些参数时,脚本将逐个打印出这些参数的值。 希望这个例子能帮助你理解bash编程中的shift命令的用法。请让我知道如果你还有其他问题。123 #### 引用[.reference_title] - *1* *2* *3* [Bash编程](https://blog.csdn.net/xiyangyang410/article/details/86695841)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值