Skip to content

exec doesn't support a list of arguments #8751

Closed

Description

@pingiun

Describe the bug

I believe it's currently impossible to do use the following pattern from bash type shells in nu:

<do things with given arguments, until reaching --, shifting away these arguments from the $@ list>
# Execute the command that's given after --
exec "$@"

This allows for a nice continuation passing style of programming, this is sometimes called Bernstein chaining. After DJB who applied this in qmail (and other shell programs I believe).

This can work in nu when using run-external, for example:

def main [...command] {
  run-external $command.0 ($command | skip 1)
}

I would like for this to be possible with exec as well. I think for exec to be the most useful it would support the same signature as run-external, such that I could replace run-external for exec in the example above. For bonus points, add the -a name option from bash exec that sets the zeroth argument name in the execve() call.

Obviously this is a breaking change to the exec command. So I came up with four different solutions to this problem:

  • Add --exec option to the run-external command when running on a unix system. This might be confusing in the docs for windows users.
  • Add exec-external or similar command which does what I want to do
  • Add a command that's similar to apply from common lisp. This would execute arbitrary commands with a list of arguments, then you could do: apply exec $command.
  • Add syntax sugar for splatting arrays, like @ in oil, or * in python. Then you could do exec *$command

How to reproduce

  1. Try to execute a list of commands with exec

Expected behavior

I can execute a list of command with exec

Screenshots

No response

Configuration

N/A

Additional context

Discussed briefly on discord: https://discord.com/channels/601130461678272522/614593951969574961/1093099484621328384

Activity

  1. pingiun commented on Apr 6, 2023

    @pingiun
    ContributorAuthor

    I would be willing to work on this, and I'm probably able to spend company time on it. Does the core team think it's a good idea?

  2. added
    category:enhancement
    on Apr 6, 2023
  3. fdncred commented on Apr 6, 2023

    @fdncred
    Contributor

    I'd be up for your 3rd or 4th bullet. Let's see what other people think.

    Just to be clear, this sound interesting to me

    • Add a command that's similar to apply from common lisp. This would execute arbitrary commands with a list of arguments, then you could do: apply exec $command.
    • Add syntax sugar for splatting arrays, like @ in oil, or * in python. Then you could do exec *$command
  4. kubouch commented on Apr 7, 2023

    @kubouch
    Contributor

    run-external is more of an internal command that is not designed to be used in normal circumstances. We can change exec to interpret list of values as args to the exec-ed command, similar to how run-external works. I think that would be the simplest solution and it's something that can be done right away.

    The apply command would do basically a similar thing but in a more complicated way. It could be used to support more commands than just exec which makes it interesting, but it would need to solve the problem of passing flags around which is quite a broad issue. For the same reason, I wouldn't add any syntax sugar yet because. These would need to be designed a bit more carefully.

  5. pingiun commented on Apr 9, 2023

    @pingiun
    ContributorAuthor

    but it would need to solve the problem of passing flags around which is quite a broad issue.

    I don't understand. Why not?

  6. pingiun commented on Apr 12, 2023

    @pingiun
    ContributorAuthor

    This seems to already work. Maybe it was added in the 0.78 release? I'm not sure but I will close the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    category:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions