博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
理解Bash shell中shift命令
阅读量:4177 次
发布时间:2019-05-26

本文共 1052 字,大约阅读时间需要 3 分钟。

1. man下的解释:

[root@desktop31 log]# man shift

...

       shift [n]
              The  positional  parameters  from n+1 ... are renamed to $1 ....
              Parameters represented by the numbers  $#  down  to  $#-n+1  are
              unset.   n  must  be a non-negative number less than or equal to
              $#.  If n is 0, no parameters are changed.  If n is  not  given,
              it  is assumed to be 1.  If n is greater than $#, the positional
              parameters are not changed.  The return status is  greater  than
              zero if n is greater than $# or less than zero; otherwise 0.

...

shift n表示把第n+1个参数移到第1个参数, 即命令结束后$1的值等于$n+1的值, 而命令执行前的前面n个参数不能被再次引用, 后面$#-n+1到$#的参数被unset, 参数的个数减少为$#-n个.

n的值不能为负数, 若n为0或大于参数个数$#则参数不变, 若n没有给定则默认为1. 当n小于0或者大于参数个数$#时shift命令的返回值大于0, 否则返回0.

2. 小例子

[root@desktop31 log]# vim test

#!/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.

[root@desktop31 log]# ./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, , .
[root@desktop31 log]#

转载地址:http://wntai.baihongyu.com/

你可能感兴趣的文章
Yocto tips (20): Yocto中qemu模拟器的使用,以zynq Cortex-A9为例
查看>>
打造嵌入式ARM Linux防火墙:1. iptables基础
查看>>
4G模块SIMCOM7100 LTE在ARM Linux下使用PPPD上网
查看>>
为小米4与小米3 Mi3 Mi4编译Cyanogenmod 12.1与13.0 (CM12与CM13) 的步骤以及错误解决
查看>>
原生Android系统的第一次开机google验证的解决
查看>>
S5P4418与S5P6618的Android boot.img的解压与压缩, Sparse ext4文件系统
查看>>
【EVB-335X-II试用体验】 u-boot与kernel的编译以及本地repo的建立
查看>>
【EVB-335X-II试用体验】 上手试用与资源使用
查看>>
【EVB-335X-II试用体验】 Yocto环境的建立及Rootfs的构建与使用
查看>>
<<C++程序设计原理与实践>>粗读--chapter0 chapter1 chapter2
查看>>
<<C++程序设计原理与实践>>粗读--chapter3 chapter4 Chapter5
查看>>
<<C++程序设计原理与实践>>粗读 -- chapter8 Chapter9
查看>>
Linux Qt程序打包成一个可执行文件
查看>>
DragonBoard 410C中的Fastboot与调试串口注意事项
查看>>
跨系统的录音格式兼容性问题: iOS Android
查看>>
JVM 的垃圾回收器
查看>>
Mybatis的缓存
查看>>
@validated注解异常返回JSON值
查看>>
@JsonFormat注解使用
查看>>
Spring boot集成jxls实现导入功能
查看>>