bug-bash
[Top][All Lists]
Advanced

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

unhelpful effect of '!' prefix on commands in conditionals - a bug ?


From: Jason Vas Dias
Subject: unhelpful effect of '!' prefix on commands in conditionals - a bug ?
Date: Fri, 13 Jun 2014 21:42:00 +0100

Having defined a function _F to return a non-zero return status :
  $ function _F () { return 255; }
I'd expect to be able to test this return status in an if clause - but
this is what happens:
  $ if ! _F ; then echo '_F returned: '$?; fi
  _F returned: 0
whereas if I just run F inline, the return status is available:
  $ _F; echo $?
   255
Interestingly, if I don't use '!' in the conditional, I can access the
the return status:
  $  if  _F ; then echo OK; else echo '_F returned: '$?; fi
  _F returned: 255

This is with bash:
  $ bash --version
  GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
  ...
  from an up-to-date copy of "Scientific Linux release 6.5" for X86_64 .

This behavior seems to me to be an instant source of confusion and bugs -
does anyone agree with me that this is a bug ?

Is this really mandated by the standards ?

Is there some other variable I could test to retrieve $? if it has
been mangled by a '!'
in the conditional ?

Is there any other conclusion than : "if you want to access the return
status of a
function in an if clause , don't use '!' in the conditional" ?

Any responses / suggestions gratefully received.

Thanks & Regards,
Jason



reply via email to

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