0

How can I iterate over my function parameters starting with the second parameter?

#!/bin/bash -

function test23() {
  echo 'hello world'

  for text in $2..$@
  do
   echo $text
   done
}

test23 start with the second argument

My current output is

hello world
with..start
with
the
second
argument

and I want to get 'with the second argument'.

user5580578
  • 1,134
  • 1
  • 12
  • 28

2 Answers2

1

as per stack-overflow: Iterate through parameters skipping the first

function test23() {
  echo 'hello world'
  for text in "${@:2}"; do
    echo $text
  done
}

$ test23 start with the second argument
hello world
with
the
second
argument
Jad
  • 1,257
  • 12
  • 19
1

You can shift:

#!/usr/bin/env bash

test23() {
  echo 'hello world'

  shift

  for text
  do
    printf '%s\n' "$text"
  done
}
Biffen
  • 6,249
  • 6
  • 28
  • 36