Skip to main content
added 335 characters in body
Source Link
Kusalananda
  • 356.6k
  • 42
  • 739
  • 1.1k

The script terminates upon returning from func, since its exit status is non-zero. The script is not terminating inside func.

The false && true list is unaffected by -e, and the script does not terminate from it, not in the main part of the script and not in the function.

However, the false in the function sets the function's exit status to non-zero, so when the function returns, the shell terminates.

Your script may be simplified into

#!/bin/bash -e false && true echo "1" false echo "2" 

You may also want to test returning zero from your function to convince yourself that the false && true list in the function is not terminating the script:

#!/bin/bash -e func() { false && true return 0 } false && true echo "1" func echo "2" 

Running this outputs

1 2 

The script terminates upon returning from func, since its exit status is non-zero. The script is not terminating inside func.

The false && true list is unaffected by -e, and the script does not terminate from it, not in the main part of the script and not in the function.

However, the false in the function sets the function's exit status to non-zero, so when the function returns, the shell terminates.

Your script may be simplified into

#!/bin/bash -e false && true echo "1" false echo "2" 

The script terminates upon returning from func, since its exit status is non-zero. The script is not terminating inside func.

The false && true list is unaffected by -e, and the script does not terminate from it, not in the main part of the script and not in the function.

However, the false in the function sets the function's exit status to non-zero, so when the function returns, the shell terminates.

Your script may be simplified into

#!/bin/bash -e false && true echo "1" false echo "2" 

You may also want to test returning zero from your function to convince yourself that the false && true list in the function is not terminating the script:

#!/bin/bash -e func() { false && true return 0 } false && true echo "1" func echo "2" 

Running this outputs

1 2 
Source Link
Kusalananda
  • 356.6k
  • 42
  • 739
  • 1.1k

The script terminates upon returning from func, since its exit status is non-zero. The script is not terminating inside func.

The false && true list is unaffected by -e, and the script does not terminate from it, not in the main part of the script and not in the function.

However, the false in the function sets the function's exit status to non-zero, so when the function returns, the shell terminates.

Your script may be simplified into

#!/bin/bash -e false && true echo "1" false echo "2"