bash 如何跳过 $@ 中的第一个参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8906187/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:20:31  来源:igfitidea点击:

How to skip the first argument in $@?

bashshell

提问by Alexander Abashkin

My code:

我的代码:

#!/bin/bash

for i in $@;
    do echo $i;
done;

run script:

运行脚本:

# ./script 1 2 3

1
2
3

So, I want to skip the first argument and get:

所以,我想跳过第一个参数并得到:

# ./script 1 2 3

2
3

回答by SiegeX

Use the offset parameter expansion

使用偏移参数扩展

#!/bin/bash

for i in "${@:2}"; do
    echo $i
done

Example

例子

$ func(){ for i in "${@:2}"; do echo "$i"; done;}; func one two three
two
three

回答by khachik

Use shiftcommand:

使用shift命令:

FIRST_ARG=""
shift
REST_ARGS="$@"

回答by hendry

Look into Parameter Expansionsin the bash manpage.

查看bash 联机帮助页中的参数扩展

#/bin/bash
for i in "${@:2}"
    do echo $i
done

回答by Dan Fego

You could just have a variable testing whether it's the first argument with something like this (untested):

你可以有一个变量来测试它是否是这样的第一个参数(未经测试):

#!/bin/bash
FIRST=1
for i in $@
do
    if [ FIRST -eq 1 ]
    then
        FIRST=0
    else
        echo $i
    fi
done