Home > Back-end >  get n latest arguments shell script
get n latest arguments shell script

Time:02-01

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 ....

CodePudding user response:

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

CodePudding user response:

You can copy the arguments in a bash array then slice it:

#!/bin/bash

files=( "$@" )
files=( "${files[@]:6}" )

# show content of the array
declare -p files
  •  Tags:  
  • Related