Search code examples
linuxnushell

Bash $@ equivalent in nushell


I want to access all the arguments and run the same command on each argument in the list.

#!/usr/bin/env nu

def main [ one, two ] {
    // echo each item in the argument list
}

How to achieve this in nushell?


Solution

  • It sounds like you are looking for Rest parameters:

    def main [...args: string] {
        $args| each { echo $in }
    }
    
    > main one two three
    ╭───┬───────╮
    │ 0 │ one   │
    │ 1 │ two   │
    │ 2 │ three │
    ╰───┴───────╯
    

    args can be named whatever you want. The only "magic" variable in that example is $in, which could be replaced with the non-magic form:

        $args| each { |item| echo $item }