Why is this bash script not coming out?

This may be a basic question, but I'm not sure why logging will cause this exit function to not work properly:

#!/bin/bash

function exitFunct
{
  exit 1
}

exitFunct  2>&1 | tee -a /var/tmp/output.txt
echo "You should never see this"

But the conclusion, "You will never see this"

+4
source share
1 answer

As man bashexplains

Each command in the pipeline is executed as a separate process (i.e., in a subshell).

Consequently, exitthe function only leaves a subshell, which starts the functional part of the pipeline.

As well as

The return status of the pipeline is the exit status of the last command, if the option is not enabled pipefail.

Therefore, you can change the behavior by adding

set -eo pipefail

script (-e script ). , , exit 0 .

+7

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


All Articles