简体   繁体   English

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

[英]How to skip the first argument in $@?

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

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

Use shift command: 使用shift命令:

FIRST_ARG="$1"
shift
REST_ARGS="$@"

Look into Parameter Expansions in the bash manpage. 查看bash联机帮助页中的参数扩展

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM