You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To summarize, we have three ways of doing multiple commands in most other shells:
a; b, always run both a and b
a && b, only run b if a was successful
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 🙂
Reacted by Theo Paris, Tasty Chips Electronics, Jonas Gollenz, Jack Wright, gralpli, Simone Fidanza, Maxwell Anderson, Rahul Ahuja, Jeffrey Ock, Tux3 and 7 more
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 '&&'
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:
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:
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).
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:
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:
printhello; ^false|^true; printworld# => 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:
defpipefail [] {
letresults=completematch$results {
{exit_code:0} =>$results.stdout,
_=> (error make { msg:"External command had a non-zero exit code" })
}
}
printhello; ^false|pipefail|^true; printworld# => 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 │ }# => ╰────
Reacted by Stefan Holderbach, Darren Schroeder, Daniel Carosone, WieeRd, Jacek Chmielewski, Ben Mellor, Diogo Castro, Money and Wakunguma Kalimukwa
Reacted by Stefan Holderbach, Darren Schroeder, Xavier Ruiz, Diogo Castro and Money
Activity
sophiajt commentedon Sep 30, 2019
This isn't yet supported, but we'd like to add support for it, or something like it.
andrasio commentedon Nov 2, 2019
#900
thegedge commentedon Feb 6, 2020
To summarize, we have three ways of doing multiple commands in most other shells:
a; b, always run bothaandba && b, only runbifawas successfula || b, only runbifawas unsuccessfulWe 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,
nucould take a completely different approach to this without sacrificing a good user experience. Something to think about 🙂aeosynth commentedon May 3, 2020
nu 0.13 has semicolon support, so this code works now:
ShikiSuen commentedon Dec 20, 2021
But "&&" is still not supported yet.
gralpli commentedon Jun 3, 2022
sudo apt update && sudo apt list --upgradablewas literally the first command I tried after installation. I guess Nushell is still not ready yet.Disable prefetch setting (nushell#740)
debemdeboas commentedon Feb 28, 2023
Any updates on this?
kpeterstech commentedon Feb 28, 2023
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.0good-unit commentedon Mar 6, 2023
; is not a replacement of &&
# The ls command (below) still executes in nushell (when it shouldn't with &&).
1 + 1 == 3 ; ls
heiytor commentedon Jul 31, 2023
Did you find any solution? I'm trying the same
mkcdcommand.amtoine commentedon Jul 31, 2023
@good-unit @heiytor
i think one solution for the
mkcdcommand is the followingdef-envallowscdto mutate the environment, changing$env.PWDand thus allowing to change directorydirectoryis apathmkcdtakesnothingas input and outputsnothingmkdirandcdasmkdirwill never fail (unless some bad IO / disk error of course)😋
ted-dev-42 commentedon Aug 15, 2023
; is not working, only the last command has been executed, why?
sophiajt commentedon Aug 15, 2023
@ted-dev-42 use
printto print to the screenhinricht commentedon Sep 5, 2023
Why is
echobehaving like this ? I found this because I stumbled across this as well, and it seeems counter-intuitive to me:amtoine commentedon Sep 5, 2023
@hinricht
this is a source of confusion i propose to remove in @10221 and a followup removal PR later 👍
tomatau commentedon Feb 17, 2024
This is
def --envnow, for anyone else who got stumped here.Atemu commentedon May 8, 2024
So what exactly is the status on this? Has this been implemented?
WieeRd commentedon Jun 11, 2024
For people who are wondering, as a workaround you can manually check for
$env.LAST_EXIT_CODE.I definitely want to see a dedicated syntax being added to Nushell, though.
132ikl commentedon Nov 15, 2024
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 / 0as an example of a simple internal command which errors, you can see execution stops after1 / 0is evaluated: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: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).
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 upgradein Bash is equivalent tosudo apt update; sudo apt upgradein Nushell. Ifsudo apt updatefails, anu::shell::non_zero_exit_codeerror will be thrown, stopping execution.This is a bit different from the previous example, since the operation completes successfully, but returns a
falsevalue. Therefore, you should check whether the value is true or false. You could write this as:Or, if you wish to run this as part of a pipeline, you could do something like this:
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
pipefailoption 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: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:
WieeRd commentedon Nov 15, 2024
Now we just need
||equivalent in Nushell.weirdan commentedon Nov 16, 2024
try { .... } catch { .... }is the closest Nushell currently has.Atemu commentedon Nov 16, 2024
It'd be great to have a shorthand for this (IMHO quite common) pattern.
egirlcatnip commentedon Feb 12, 2025
Are there any updates on this? I'm still missing the && and the || operators, even if they were just shorthands:
proposition:
philocalyst commentedon Mar 1, 2026
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
c-git commentedon Mar 1, 2026
It kinda does... Fail early and what not. If the first command fails the second won't run