Bash flow control using || by function, with the set -e

If I'm set -ein a Bash script, the script will terminate on future errors. I am confused about how this works with functions. Consider the following, which will only output the onestandard:

set -e # Exit on error
fun(){
    echo one
    non_existing_command
    echo two
}
fun

It is clear that this non_existing_commandis a mistake, and therefore the script exits before the second echo. You can usually use an operator or ||execute another command if and only if the first command fails. That is, I would suspect the following to print as oneand three, but not two:

set -e # Exit on error
fun(){
    echo one
    non_existing_command
    echo two
}
fun || echo three

one two. || ( ), .

?

+5
1

, set

, -e [, || ], , , -e, -e .

.

, set -e , : :

-e , -e , , , , .

+6

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


All Articles