In Bash I would do something like
...
...
if ! cd non_exist_dir > /dev/null 2>&1; then
echo "error in cd"
return 1
fi
...
...
How would I do that in idiomatic nushell ?
Especially, how can I get rid of the annoying error message displayed by nushell because the directory doesn't exist ?
Bonus points if I could get something like a "return" command, which seems to be missing in nushell.
Nushell 0.72 introduces
try/catchblocks which can suppress or replace the error handling for Nushell builtins. For your example:As for the other part of your question, a return code would only seem to be useful if are returning to a non-Nushell process. Any error handling from within Nushell can use Nushell errors and
try/catchblocks.But if you are returning to a non-Nushell process, then you can use the
exit <status_code>builtin.This would only work in either:
nu -c "<commands>"However, a simple
cdcould never work in either case, since both cases start a new Nushell shell (try saying that 10 times real fast), and when that shell exits, the environment will be returned to its entry state (even withdef-env).But if you are doing something after the
cd, then it would make sense to include this in a script. For example, asilly_cd.nu(silly, because it's pretty useless other than as a demonstration ofexit):