[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: warning rookie question - how to get return value
|
From: |
Laurent Lyaudet |
|
Subject: |
Re: warning rookie question - how to get return value |
|
Date: |
Tue, 3 Nov 2026 23:12:52 +0100 |
Le mar. 3 nov. 2026 à 22:38, Greg Wooledge <greg@wooledge.org> a écrit :
>
> On Tue, Nov 03, 2026 at 12:04:10 -0600, grumpy@manlymail.net wrote:
> > read x < <(do_my_app)
> >
> > how do i get the return value for do_my_app
>
> Do you mean the exit status? In sufficiently new versions of bash,
> you can explicitly wait for it:
>
> hobbit:~$ read -r x < <(echo true; exit 37)
> hobbit:~$ wait $!
> hobbit:~$ echo $?
> 37
>
> This requires bash 4.4 or newer. If you need the exit status of that
> command in an older version of bash, you'll need to throw away the process
> substitution syntax, and use a named pipe and explicit background jobs.
>
Then the answer I would prefer is
https://www.gnu.org/software/bash/manual/bash.html#index-PIPESTATUS
do_my_app | { read x; }
echo ${PIPESTATUS[0]}
Example :
laurent@laurent-GL73-8SD:~$ function doh(){ echo "whatever"; return 111; }
laurent@laurent-GL73-8SD:~$ doh | { read x; }
laurent@laurent-GL73-8SD:~$ echo ${PIPESTATUS[0]}
111