Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip the first argument in $@?

Tags:

bash

shell

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 
like image 383
Alexander Abashkin Avatar asked Jan 18 '12 06:01

Alexander Abashkin


People also ask

How do you pass arguments in a script?

Using arguments Inside the script, we can use the $ symbol followed by the integer to access the arguments passed. For example, $1 , $2 , and so on. The $0 will contain the script name.

What is argument in bash script?

A command line argument is a parameter that we can supply to our Bash script at execution. They allow a user to dynamically affect the actions your script will perform or the output it will generate.


2 Answers

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 
like image 164
SiegeX Avatar answered Sep 24 '22 13:09

SiegeX


Use shift command:

FIRST_ARG="$1" shift REST_ARGS="$@" 
like image 43
khachik Avatar answered Sep 24 '22 13:09

khachik