News for hackers. Stuff that matters.
Re:Why doesn't Bash's `set -e` do what I expected? by dataflow on Friday October 07, 2022 @12:02AM (#33117279)
Yeah. Basically instead of treating ! specially, Bash treats it like any other command that sets $? afterwards. And since it always succeeds, you always end up with $?=0 after it.

I don't know what kind of shell scripts the designers of this write, but the number of times I've wanted or found it helpful for ! to modify $? is has been exactly zero.

I am almost certain that >99% of people who write shell scripts do not find this intuitive.

Re:Why doesn't Bash's `set -e` do what I expected? by trashtester on Friday October 07, 2022 @05:06AM (#33118980)
> Yeah. Basically instead of treating ! specially, Bash treats it like any other command that sets $? afterwards. And since it always succeeds, you always end up with $?=0 after it.

I don't think that is true, consider:

  # if  false ; then  echo "true $?" ; else echo "false $?" ; fi
  false 1
  # if ! false ; then  echo "true $?" ; else echo "false $?" ; fi
  true 0
  # if ! ! false ; then  echo "true $?" ; else echo "false $?" ; fi
  false 1
Basically, $? evaluates to the full condition inside the "if" sentence.

One of the first things I learned when starting to use $? was to assign it to a variable asap if I needed to use it more than once.

Re:Why doesn't Bash's `set -e` do what I expected? by dataflow on Friday October 07, 2022 @09:47AM (#33121242)
Whoops, that's what I get for writing these when I'm sleepy. Yes of course ! doesn't always set the result to success, that wouldn't make any sense. I misspoke in that part. I meant to say Bash treats it like any other command that modifies $?, instead of treating it specially and preventing it from modifying that variable. Thanks.