help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Looking for something like "errexit" for interactive ses


From: Greg Wooledge
Subject: Re: [Help-bash] Looking for something like "errexit" for interactive sessions
Date: Wed, 28 Aug 2019 08:50:08 -0400
User-agent: Mutt/1.10.1 (2018-07-13)

On Tue, Aug 27, 2019 at 02:42:16PM -0700, Eric Pruitt wrote:
> Is there a feature akin to errexit that's interactive session friendly
> i.e. instead of causing the session to exit, it'd drop the user back to
> a prompt if a command finished with a non-zero status?

Every command drops the user back to the prompt when it finishes, whether
the exit status was 0 or non-zero.

If you want, you can add some expansions in PS1 to tell you what the exit
status was.  Some people find that useful.  For instance,

wooledg:~$ PS1='[$?] \h:\w\$ '
[0] wooledg:~$ true
[0] wooledg:~$ false
[1] wooledg:~$ (exit 37)
[37] wooledg:~$ 

Or, if you have a color fetish,

wooledg:~$ cc=( "$(tput setaf 2)" "$(tput setaf 1)" ) norm=$(tput sgr0)
wooledg:~$ PS1='[\[${cc[!!$?]}\]$?\[$norm\]] \h:\w\$ '
[0] wooledg:~$ (exit 11)
[11] wooledg:~$ 

You can't see it in this email, of course, but the 0 is green and
the 11 is red.  !!$? in an arithmetic context does a logical negation
of the exit status twice, which maps 0 to 0, and non-zero to 1.



reply via email to

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