Why not set - causes a crash with `false || false && true`?

It is not possible to determine a suitable name, I do not understand the behavior in the dash / bash. Namely, I use set -e to save if a command and commands fail to process a positive result.

T. General scheme:

[ ! wantcommand ] || command

Than means that the command is run only when necessary, and failure will automatically disable the script.

It may take some post-processing, in which case I use this:

[ ! wantcommand ] || { command && postprocess; }

This led to some curious bughunting, since this one will not kill the shell , and I cannot end this reason. Now I need to execute some pieces of shell code, but I would like to understand the reason.

for testing:

bash -c 'set -e; { false || false && echo "post" ; }; echo "ec $?"'

or

bash -c 'set -e; { set -e; false || false && echo "post" ; }; echo "ec $?"'

. , , 1,

+4
1

set -e .

( if, until, while, && ||), .

, , .


:

, - ( , ), , , exit , :

  • - . .

  • -e , while, until, if elif, , ! AND-OR, .

  • , , , -e , -e .

. , :

set -e; (false; echo one) | cat; echo two

false echo one; echo two , (false; echo one) | cat .

, ; , POSIX, .


- , BashFAQ # 105 , set -e , . FVUE wiki set -e bash - POSIX, .

+11

Source: https://habr.com/ru/post/1660420/


All Articles