bashshell

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

Solution

  • 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