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

0

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

1

You can shift:

#!/usr/bin/env bash

test23() {
  echo 'hello world'

  shift

  for text
  do
    printf '%s\n' "$text"
  done
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.