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
As I read through #5347, #3858, #5740, #6667, #7076, #6617, there is no native or even clean way to support the bash behavior cmd1 || cmd2.
Describe the solution you'd like
I would like to propose a way to solve this without big changes to the syntax or the pipeline mode of nushell. We just need a command that can convert an exit code to an boolean or error. Therefore I propose a new command (either named test or error test) to do this:
nu -c 'exit 0' | test will become true.
nu -c 'exit -1' | test will become false.
nu -c 'exit -1' | test -c 1 will become true. (it only return false when the exit code is 1)
nu -c 'exit -1' | test -e will become a nu error
nu -c 'exit -1' | test -e {msg: "non-zero return"} will become a nu error with custom message
With this command, the bash behavior of cmd1 || cmd2 will become (cmd1 | test) || (cmd2 | test), which is still elegant in my opinion. The behavior of cmd1 && cmd2 can also be supported this way.
Describe alternatives you've considered
One solution is to support the behavior natively as tried by #6374. But it turns out to be a big change and it might affect other scripts running right now.
There is also a workaround a I mentioned in #6667, we can use following sentences
All of these are less intuitive, and the problem is that the stdout and stderr of cmd1 will be captured by the complete command instead of being printed out.
Additional context and details
No response
The text was updated successfully, but these errors were encountered:
I believe the Bash || operator is intended to be added before 0.80, according to this.
I actually think that, because nushell is a new shell designed from scratch, we can do better than bash. For the behavior of || and &&, we can definitely support it, but the functionality is still limited. The proposed command can convert a non-zero exit code to a nushell error, and it can raise error when specific exit code is returned.
For the behavior of >, >> and 2>, etc., I think nushell should not support them at all. These symbols caused me a lot of confusion when I first try to learn bash. I actually prefer just to use the save command for this. (though I'm not sure if there is a way to redirect stderr to stdout in nushell yet)
Related problem
As I read through #5347, #3858, #5740, #6667, #7076, #6617, there is no native or even clean way to support the bash behavior
cmd1 || cmd2
.Describe the solution you'd like
I would like to propose a way to solve this without big changes to the syntax or the pipeline mode of nushell. We just need a command that can convert an exit code to an boolean or error. Therefore I propose a new command (either named
test
orerror test
) to do this:nu -c 'exit 0' | test
will becometrue
.nu -c 'exit -1' | test
will becomefalse
.nu -c 'exit -1' | test -c 1
will becometrue
. (it only return false when the exit code is 1)nu -c 'exit -1' | test -e
will become a nu errornu -c 'exit -1' | test -e {msg: "non-zero return"}
will become a nu error with custom messageWith this command, the bash behavior of
cmd1 || cmd2
will become(cmd1 | test) || (cmd2 | test)
, which is still elegant in my opinion. The behavior ofcmd1 && cmd2
can also be supported this way.Describe alternatives you've considered
One solution is to support the behavior natively as tried by #6374. But it turns out to be a big change and it might affect other scripts running right now.
There is also a workaround a I mentioned in #6667, we can use following sentences
if (cmd1 | complete).exit_code != 0 { cmd2 }
if (cmd1 | complete | $in.exit_code) != 0 { cmd2 }
All of these are less intuitive, and the problem is that the stdout and stderr of
cmd1
will be captured by thecomplete
command instead of being printed out.Additional context and details
No response
The text was updated successfully, but these errors were encountered: