Gamaor
Gamaor

Reputation: 145

get n latest arguments shell script

So I have some script

files="${!#}"
if [[ -n "$files" ]]
then
        do some instructions
else
        do some instructions
fi

I really need to how to get n arguments from the 7th argument or N argument. Something like this:

var1=arg1 arg2 arg3 .... arg7 argN argN+1 ...

Capture them in a variable like this:

var1= argN argN+1 ....

Upvotes: 1

Views: 1187

Answers (2)

Fravadona
Fravadona

Reputation: 17208

You can use a bash array and then slice it:

As @GordonDavisson pointed out, you can directly slice $@

#!/bin/bash

files=( "${@:7}" )

# show content of the array
declare -p files

Upvotes: 4

Barmar
Barmar

Reputation: 781751

Use shift to remove the first N-1 arguments, then $@ will contain the remaining arguments.

shift 6
if [ $# -ne 0 ]
then
    files=("$@")
    # do something with $files
else
    # do something else
fi

Upvotes: 4

Related Questions