Skip to content

How to pass variable length of arguments to other commands #7758

@r12f

Description

@r12f

Question

Hi, I really like nushell and would like to use it for my daily shell, but I met some problems when I am trying to create some aliases in nushell, specifically - how to create an alias that accepts variable parameters. Could anyone help me take a look?

It might be a dumb question, but I am kinda stuck. I did some searches and have tried several ways below to do it, but none of them works.

First, I tried to use alias keyword (https://www.nushell.sh/book/aliases.html#aliases):

C:\Users\r12f\Downloads〉alias ll = (ls -l)                                                                                                                                                01/14/2023 10:56:56 PM
C:\Users\r12f\Downloads〉ll *.zip                                                                                                                                                          01/14/2023 11:00:21 PMError: nu::parser::parse_mismatch (link)

  × Parse mismatch during operation.
   ╭─[entry #58:1:1]
 1 │ ll *.zip
   ·    ──┬──
   ·      ╰── expected operator
   ╰────

Then I have tried to use custom commands with rest parameters (https://www.nushell.sh/book/custom_commands.html#rest-parameters):

C:\Users\r12f\Downloads〉def lsl [...x] { ls -l $x }                                                                                                                                       01/14/2023 10:55:56 PMC:\Users\r12f\Downloads〉lsl *.zip                                                                                                                                                         01/14/2023 10:56:52 PM
Error: nu::shell::cant_convert (link)

  × Can't convert to string.
   ╭─[entry #55:1:1]
 1 │ def lsl [...x] { ls -l $x }
   ·                        ─┬
   ·                         ╰── can't convert list<string> to string
   ╰────

With some searches, I found a few threads discussing similar things (e.g. #1716), so I have also tried the syntax there, but it looks like the syntax is already changed:

C:\Users\r12f\Downloads〉alias ll [x...] { ls -l $x }                                                                                                                                      01/14/2023 11:00:23 PMError: nu::parser::assignment_mismatch (link)

  × alias missing sign
   ╭─[entry #59:1:1]
 1 │ alias ll [x...] { ls -l $x }
   ·          ───┬──
   ·             ╰── missing equal sign
   ╰────

So I am wondering what would be the right way to do it and is there any doc for this?

Really appreciate for the help in advance!

Additional context and details

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    deprecated:questionQuestions should be redirected to GitHub discussions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions