首页 > 解决方案 > 从第二个函数参数开始的 Ruby for 循环

问题描述

如何从第二个参数开始迭代我的函数参数?

#!/bin/bash -

function test23() {
  echo 'hello world'

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

test23 start with the second argument

我目前的输出是

hello world
with..start
with
the
second
argument

我想得到“第二个论点”。

标签: bash

解决方案


你可以shift

#!/usr/bin/env bash

test23() {
  echo 'hello world'

  shift

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

推荐阅读