Skip to content

How do I separate multiple commands? #740

Closed

Description

@alper

Is there a way to separate multiple commands such as I would normally do with a semicolon in bash?

git pull; git push

Would it make sense to add a feature like that?

Activity

  1. sophiajt commented on Sep 30, 2019

    @sophiajt
    Contributor

    This isn't yet supported, but we'd like to add support for it, or something like it.

  2. andrasio commented on Nov 2, 2019

    @andrasio
    Member
  3. added
    category:enhancement
    on Nov 2, 2019
  4. thegedge commented on Feb 6, 2020

    @thegedge
    Contributor

    To summarize, we have three ways of doing multiple commands in most other shells:

    1. a; b, always run both a and b
    2. a && b, only run b if a was successful
    3. a || b, only run b if a was unsuccessful

    We don't really need (1) or (2) in scripts, but they're useful on the command-line . For example, when I go back through my history, I like using one-liners to re-run some set of commands. That being said, nu could take a completely different approach to this without sacrificing a good user experience. Something to think about 🙂

  5. aeosynth commented on May 3, 2020

    @aeosynth
    Contributor

    nu 0.13 has semicolon support, so this code works now:

    git pull; git push
    
  6. ShikiSuen commented on Dec 20, 2021

    @ShikiSuen

    But "&&" is still not supported yet.

  7. gralpli commented on Jun 3, 2022

    @gralpli

    sudo apt update && sudo apt list --upgradable was literally the first command I tried after installation. I guess Nushell is still not ready yet.

  8. debemdeboas commented on Feb 28, 2023

    @debemdeboas

    Any updates on this?

  9. kpeterstech commented on Feb 28, 2023

    @kpeterstech

    I am having problems getting this function to run. It will make the directory called $name, but the cd command won't run. If I try to use && I get this error. I am using version 0.76.0

    def mkcd [name: string] {
        mkdir $name; cd $name
        }
    
    Error: nu::parser::shell_andand (link)
    
      × The '&&' operator is not supported in Nushell
         ╭─[config.nu:653:1]
     653 │ def mkcd [name: string] {
     654 │     mkdir $name && cd $name
         ·                 ─┬
         ·                  ╰── instead of '&&', use ';' or 'and'
     655 │     }
         ╰────
      help: use ';' instead of the shell '&&', or 'and' instead of the boolean '&&'
    
  10. good-unit commented on Mar 6, 2023

    @good-unit

    ; is not a replacement of &&

    # The ls command (below) still executes in nushell (when it shouldn't with &&).
    1 + 1 == 3 ; ls

  11. heiytor commented on Jul 31, 2023

    @heiytor

    ; is not a replacement of &&

    The ls command (below) still executes in nushell (when it shouldn't with &&). 1 + 1 == 3 ; ls

    Did you find any solution? I'm trying the same mkcd command.

  12. amtoine commented on Jul 31, 2023

    @amtoine
    Member

    @good-unit @heiytor
    i think one solution for the mkcd command is the following

    def-env mkcd [directory: path]: nothing -> nothing {
        mkdir $directory
        cd $directory
    }
    1. def-env allows cd to mutate the environment, changing $env.PWD and thus allowing to change directory
    2. directory is a path
    3. mkcd takes nothing as input and outputs nothing
    4. then we do not need any fancy logic between mkdir and cd as mkdir will never fail (unless some bad IO / disk error of course)

    😋

  13. ted-dev-42 commented on Aug 15, 2023

    @ted-dev-42

    ; is not working, only the last command has been executed, why?

    echo hello; echo world
    world

  14. sophiajt commented on Aug 15, 2023

    @sophiajt
    Contributor

    @ted-dev-42 use print to print to the screen

  15. hinricht commented on Sep 5, 2023

    @hinricht

    Why is echo behaving like this ? I found this because I stumbled across this as well, and it seeems counter-intuitive to me:

    ~> print one; print two 
    one
    two
    
    ~> echo one; echo two
    two
    
  16. amtoine commented on Sep 5, 2023

    @amtoine
    Member

    @hinricht
    this is a source of confusion i propose to remove in @10221 and a followup removal PR later 👍

  17. tomatau commented on Feb 17, 2024

    @tomatau

    @good-unit @heiytor i think one solution for the mkcd command is the following

    def-env mkcd [directory: path]: nothing -> nothing {
        mkdir $directory
        cd $directory
    }

    This is def --env now, for anyone else who got stumped here.

  18. Atemu commented on May 8, 2024

    @Atemu

    So what exactly is the status on this? Has this been implemented?

  19. WieeRd commented on Jun 11, 2024

    @WieeRd

    For people who are wondering, as a workaround you can manually check for $env.LAST_EXIT_CODE.

    sudo apt update
    if $env.LAST_EXIT_CODE == 0 { sudo apt upgrade }

    I definitely want to see a dedicated syntax being added to Nushell, though.

  20. 132ikl commented on Nov 15, 2024

    @132ikl
    Member

    This thread was linked to recently regarding && syntax, so I wanted to make sure this thread has updated information for anyone who might find it in the future. (It would serve us well to have actual documentation on this as well..)

    Fundamentally, execution stops when an error is thrown. Therefore, if you have multiple commands in series (either in multiple separate lines in a script or separated by a semicolon), and one throws an error, then execution will stop.

    When internal commands are unsuccessful, they throw an error. Using the expression 1 / 0 as an example of a simple internal command which errors, you can see execution stops after 1 / 0 is evaluated:

    print hello; 1 / 0; print world
    # => hello
    # => Error: nu::shell::division_by_zero
    # =>
    # =>   × Division by zero.
    # =>    ╭─[entry #36:1:16]
    # =>  1 │ print hello; 1 / 0; print world
    # =>    ·                ┬
    # =>    ·                ╰── division by zero
    # =>    ╰────

    When external commands are unsuccessful (non-zero exit code), they also throw an error since #13515 (September 2024). This error isn’t displayed by default, but you can enable it with $env.config.display_errors.exit_code = true:

    $env.config.display_errors.exit_code = true
    ^false
    # => Error: nu::shell::non_zero_exit_code
    # =>
    # =>   × External command had a non-zero exit code
    # =>    ╭─[entry #11:1:2]
    # =>  1 │ ^false
    # =>    ·  ──┬──
    # =>    ·    ╰── exited with code 1
    # =>    ╰────

    Therefore, an external command with a non-zero exit code will interrupt execution as well (using all externals here for sake of completeness, but print works as well).

    ^echo hello; ^false; ^echo world
    # => hello
    # => Error: nu::shell::non_zero_exit_code
    # =>
    # =>   × External command had a non-zero exit code
    # =>    ╭─[entry #14:1:15]
    # =>  1 │ ^echo hello; ^false; ^echo world
    # =>    ·               ──┬──
    # =>    ·                 ╰── exited with code 1
    # =>    ╰────

    Of course, the above example also works with $env.config.display_errors.exit_code = false, since that only affects whether the error is displayed.

    Because of this, a && operator in Nushell would be redundant, since this is now how Nushell works in the first place (as of September at least). Taking a closer look at a couple examples from this thread, here’s how you can write them in Nushell:

    sudo apt update && sudo apt upgrade in Bash is equivalent to sudo apt update; sudo apt upgrade in Nushell. If sudo apt update fails, a nu::shell::non_zero_exit_code error will be thrown, stopping execution.
     

    1 + 1 == 3; ls

    This is a bit different from the previous example, since the operation completes successfully, but returns a false value. Therefore, you should check whether the value is true or false. You could write this as:

    if (1 + 1 == 3) { ls }

    Or, if you wish to run this as part of a pipeline, you could do something like this:

    def and-then [func: closure] {
      if $in {
        do $func
      }
    }
    1 + 1 == 3 | and-then { ls }

     

    There is one important caveat still, which is that externals which somewhere other than the end of a pipeline will not throw an error. This is equivalent to bash’s pipefail option being disabled. This is due to a limitation in the code (see #13817), and is definitely a footgun which I hope to see resolved. Here’s an example where this causes issues:

    print hello; ^false | ^true; print world
    # => hello
    # => world

    If you need a workaround for this, I have somewhat of a miserable solution, but it is better than nothing. Note that this also collects the input, so streaming with this workaround isn’t possible:

    def pipefail [] {
      let results = complete
      match $results {
        {exit_code: 0} => $results.stdout,
        _ => (error make { msg: "External command had a non-zero exit code" })
      }
    }
    
    print hello; ^false | pipefail | ^true; print world
    # => hello
    # => Error:   × External command had a non-zero exit code
    # =>    ╭─[entry #1:5:11]
    # =>  4 │     {exit_code: 0} => $results.stdout,
    # =>  5 │     _ => (error make { msg: "External command had a non-zero exit code" })
    # =>    ·           ─────┬────
    # =>    ·                ╰── originates from here
    # =>  6 │   }
    # =>    ╰────
  21. WieeRd commented on Nov 15, 2024

    @WieeRd

    Now we just need || equivalent in Nushell.

  22. weirdan commented on Nov 16, 2024

    @weirdan
    Contributor

    Now we just need || equivalent in Nushell.

    try { .... } catch { .... } is the closest Nushell currently has.

  23. Atemu commented on Nov 16, 2024

    @Atemu

    It'd be great to have a shorthand for this (IMHO quite common) pattern.

  24. egirlcatnip commented on Feb 12, 2025

    @egirlcatnip

    Are there any updates on this? I'm still missing the && and the || operators, even if they were just shorthands:

    proposition:

    command1 && command2 is just an alias to command1; command2
    
    command1 || command2 is syntax sugar for try {command1} catch {command2}
    
    
  25. philocalyst commented on Mar 1, 2026

    @philocalyst
    Contributor

    I disagree with the && being an alias to ; as I believe the semicolon doesn't care about fail status -- but the || for try catch is cool :O

  26. c-git commented on Mar 1, 2026

    @c-git

    It kinda does... Fail early and what not. If the first command fails the second won't run

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