Home > OS >  Ignore specific exit code in zsh and bash
Ignore specific exit code in zsh and bash

Time:11-18

I am looking for a way to ignore a specific exit code that works across zsh and bash.

In my example I want to ignore the exit code 134. I have managed to come up with a working example for bash:

node fail.js || STATUS=$? && (if [ $STATUS == 134 ]; then true; else exit $STATUS; fi)

Any improvements on that are appreciated as well, but my struggle begins with zsh. This command won’t work in zsh. I have found out, that || is used in zsh to split commands, but I haven’t found an equivalent for the way I want it to work.

CodePudding user response:

If your intent is for the script to keep running should the node interpreter not fail, this might look like:

node fail.js || { rc=$?; if [[ $rc != 134 ]]; then exit "$rc"; fi; }

CodePudding user response:

your_command || (exit "$(($? == 134 ? 0 : $?))")

This uses exit in a subshell to change an exit status of 134 to 0, or else pass through the value if your_command fails.

$(( expr )) is for shell arithmetic expansion. Within this context, operators such as binary == and the conditional ?: ternary are standard.

I don't think that || would be an issue in zsh. Perhaps using the non-standard == operator with [ instead of [[. Zsh tries to conform to the POSIX standard when using [ or test.

man zshbuiltins says of test/[:

The command attempts to implement POSIX and its extensions where these are specified. Unfortunately there are intrinsic ambiguities in the syntax; in particular there is no distinction between test operators and strings that resemble them. The standard attempts to resolve these for small numbers of arguments (up to four); for five or more arguments compatibility cannot be relied on. Users are urged wherever possible to use the [[ test syntax which does not have these ambiguities.

  • Related