linux-command/command/shift.md

56 lines
785 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

shift
===
移动位置参数。
## 概要
```shell
shift [n]
```
## 主要用途
- 将位置参数`$n, $n+1...`重命名为`$1, $2...`。
## 参数
n可选大于等于1且小于等于参数个数的整数默认为1。
## 返回值
返回成功除非n大于参数个数或n小于1以及其他非法值。
## 例子
假设我们的脚本文件test.sh如下
```shell
#!/usr/bin/env bash
# 显示前三个位置参数。
echo "$1 $2 $3"
# 移除前两个位置参数,并将$3重命名为$1之后的以此类推。
shift 2
echo "$1 $2 $3"
```
在终端执行该脚本:
```shell
sh test.sh q w e r t
```
返回信息如下:
```shell
q w e
e r t
```
### 注意
1. 该命令是bash内建命令相关的帮助信息请查看`help`命令。