use*_*622 19 linux shell shift
在shell中我们有命令移位,但我在一些例子中看到它给出了移位3
为什么班次后有一个数字?它有什么关系?它能做什么 ?
例:
echo “arg1= $1 arg2=$2 arg3=$3”
shift
echo “arg1= $1 arg2=$2 arg3=$3”
shift
echo “arg1= $1 arg2=$2 arg3=$3”
shift
echo “arg1= $1 arg2=$2 arg3=$3”
shift
Run Code Online (Sandbox Code Playgroud)
输出将是:
arg1= 1 arg2=2 arg3=3
arg1= 2 arg2=3 arg3=
arg1= 3 arg2= arg3=
arg1= arg2= arg3=
Run Code Online (Sandbox Code Playgroud)
但是当我添加它时,它不能正确显示它.
dog*_*ane 47
看一下手册页,其中说:
shift [n]
The positional parameters from n+1 ... are renamed to $1 ....
If n is not given, it is assumed to be 1.
Run Code Online (Sandbox Code Playgroud)
示例脚本:
#!/bin/bash
echo "Input: $@"
shift 3
echo "After shift: $@"
Run Code Online (Sandbox Code Playgroud)
运行:
$ myscript.sh one two three four five six
Input: one two three four five six
After shift: four five six
Run Code Online (Sandbox Code Playgroud)
这表明在换了3之后$1=four,$2=five和$3=six.