help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: unexpected behavior


From: Kerin Millar
Subject: Re: unexpected behavior
Date: Thu, 12 Jan 2023 17:54:48 +0000

On Thu, 12 Jan 2023 18:24:11 +0100
Christof Warlich <cwarlich@gmx.de> wrote:

> Hi,
> 
> can anyone explain why the following line prints 0 instead of 1?:
> 
> $ stat=0; false || echo hi && echo ho && stat=1 | tee /dev/null; echo $stat
> hi
> ho
> 0
> 
> It does what I'd expect when removing the pipe, so it's obviously caused
> by some pipe magic going on ...:

See https://www.gnu.org/software/bash/manual/html_node/Pipelines.html. 
Paraphrasing slightly, "each command in a multi-command pipeline, where pipes 
are created, is executed in its own subshell, which is a separate process."

$ echo "parent: $BASHPID"; echo "child1: $BASHPID" | { cat; echo "child2: 
$BASHPID"; }
parent: 57102
child1: 57115
child2: 57116

So, the value of stat is not being modified in the "parent" shell, where you 
proceed to echo its value.

-- 
Kerin Millar



reply via email to

[Prev in Thread] Current Thread [Next in Thread]