Linux Shell 转移程序 - 这是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10414391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell shift procedure - What is this?
提问by user1253622
In shell we have the command shift, but i saw on some example its giving shift 3
在 shell 中,我们有命令 shift,但我在一些例子中看到它给出了 shift 3
Why there is a number after shift ? and what its about ? what it does ?
为什么班次后有一个数字?它是关于什么的?它能做什么 ?
Example:
例子:
echo “arg1= arg2= arg3=”
shift
echo “arg1= arg2= arg3=”
shift
echo “arg1= arg2= arg3=”
shift
echo “arg1= arg2= arg3=”
shift
The output will be:
输出将是:
arg1= 1 arg2=2 arg3=3
arg1= 2 arg2=3 arg3=
arg1= 3 arg2= arg3=
arg1= arg2= arg3=
But when i add that, it doesn't display it correctly.
但是当我添加它时,它没有正确显示。
回答by dogbane
Take a look at the manpage, which says:
看看手册页,它说:
shift [n]
The positional parameters from n+1 ... are renamed to ....
If n is not given, it is assumed to be 1.
An Example script:
示例脚本:
#!/bin/bash
echo "Input: $@"
shift 3
echo "After shift: $@"
Run it:
运行:
$ myscript.sh one two three four five six
Input: one two three four five six
After shift: four five six
This shows that after shifting by 3, $1=four
, $2=five
and $3=six
.
这表明在移位 3 之后,$1=four
,$2=five
和$3=six
。
回答by Oliver Charlesworth
This would be answered simply by reading either the Bash manual, or typing man shift
:
这可以通过阅读Bash 手册或键入man shift
以下内容来回答:
shift [n]
Shift the positional parameters to the left by n. The positional parameters from n+1 ... $# are renamed to $1 ... $#-n. Parameters represented by the numbers $# to $#-n+1 are unset. n must be a non-negative number less than or equal to $#. If n is zero or greater than $#, the positional parameters are not changed. If n is not supplied, it is assumed to be 1. The return status is zero unless n is greater than $# or less than zero, non-zero otherwise.
shift [n]
将位置参数向左移动 n。来自 n+1 ... $# 的位置参数被重命名为 $1 ... $#-n。由数字 $# 到 $#-n+1 表示的参数未设置。n 必须是小于或等于 $# 的非负数。如果 n 为零或大于 $#,则不会更改位置参数。如果未提供 n,则假定为 1。除非 n 大于 $# 或小于零,否则返回状态为零,否则为非零。
回答by Zava
you use man bash
to find the shift
builtin command:
你man bash
用来查找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.
移位 [n]
位置参数从 n+1 ... 重命名为 $1 .... 由数字 $# 到 $#-n+1 表示的参数未设置。n 必须是小于或等于 $# 的非负数。如果 n 为 0,则不更改任何参数。如果未给出 n,则假定为 1。如果 n 大于 $#,则不更改位置参数。如果 n 大于 $# 或小于零,则返回状态大于零;否则为 0。
回答by kalyan varma
Shift the positional parameters to the left by n. The positional parameters from n+1 ... $# are renamed to $1 ... $#-n. Parameters represented by the numbers $# to $#-n+1 are unset. n must be a non-negative number less than or equal to $#. If n is zero or greater than $#, the positional parameters are not changed. If n is not supplied, it is assumed to be 1. The return status is zero unless n is greater than $# or less than zero, non-zero otherwise.
将位置参数向左移动 n。来自 n+1 ... $# 的位置参数被重命名为 $1 ... $#-n。由数字 $# 到 $#-n+1 表示的参数未设置。n 必须是小于或等于 $# 的非负数。如果 n 为零或大于 $#,则不会更改位置参数。如果未提供 n,则假定为 1。除非 n 大于 $# 或小于零,否则返回状态为零,否则为非零。
- List item
- 项目清单
回答by Calculus
shift
treat command line arguments as a FIFO queue,
it popleft element every time it's invoked.
shift
将命令行参数视为 FIFO 队列,每次调用时都会弹出左元素。
array = [a, b, c]
shift equivalent to
array.popleft
[b, c]
, , can be interpreted as index of the array.
bash - The advantage of shift over reassign value straightforward - Stack Overflow