[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to set $?
From: |
Seth David Schoen |
Subject: |
Re: How to set $? |
Date: |
Sat, 25 Dec 2021 12:40:52 -0800 |
Jeffrey Walton writes:
> Hi Everyone,
>
> I have a script that executes two sets of commands. If the first set
> fails, I want to return the error code of the first set. For example:
>
> apt-get update && apt-get upgrade -y && apt-get dist-upgrade -y
> saved_result="$?"
> apt-get autoremove --purge -y && apt-get autoclean -y
> $?="$saved_result"
>
> How do I set $?
I'm not sure this is the most elegant solution, but you can make a
subshell with an exit command:
( exit "$saved_result" )