[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [ ! ! hey ] -> [: too many arguments
From: |
Greg Wooledge |
Subject: |
Re: [ ! ! hey ] -> [: too many arguments |
Date: |
Thu, 23 Jun 2022 19:26:19 -0400 |
On Thu, Jun 23, 2022 at 05:58:23PM -0400, Chet Ramey wrote:
> On 6/23/22 5:08 PM, Steffen Nurpmeso wrote:
> > bash -c 'if [ ! ! hey = hey ]; then echo du; fi'
> Sure. This one isn't a common idiom for shell programmers, apparently.
I've seen it used in a math context, inside PS1. Like so:
color=(
"$(tput setaf 2)" # 0 = green
"$(tput setaf 1)" # 1 = red
)
normal="$(tput sgr0)"
PS1='\[${color[!!$?]}\]$?\[$normal\] \h:\w\$ '
This writes the previous command's exit status in either green or red.
It "smashes" all nonzero exit codes to 1, using a double negation in
a C-style arithmetic context, in order to generate an array index for
the color.
I don't recall ever seeing it used in the way Steffen showed, though.