[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Check the error status of process substitution
From: |
Kerin Millar |
Subject: |
Re: Check the error status of process substitution |
Date: |
Mon, 6 Jun 2022 20:05:23 +0100 |
On Tue, 7 Jun 2022 03:25:49 +0900
Masahiro Yamada <masahiroy@kernel.org> wrote:
> My main motivation for using a process substitution is
> to reflect variable changes made in the loop
> (like 'nr_lines' in the sample code)
> to the outside of the loop.
>
> A pipe does not work that way
> since the loop is executed in a subshell.
Bash >=4.2 supports lastpipe so that could be an option for you. It requires
that job control be disabled but this is already the case for non-interactive
instances. I refer you to the example beneath.
#!/bin/bash
shopt -s lastpipe
{ echo line1; false; } | while read -r; do
captured_line=$REPLY
done
status=${PIPESTATUS[0]}
declare -p status captured_line
--
Kerin Millar