• NeatNit@discuss.tchncs.de
    link
    fedilink
    arrow-up
    10
    ·
    11 hours ago

    Took me a while to understand what you’re warning against. Allow me to rephrase, and tell me if I got it wrong.

    The || will execute what’s after it in two cases:

    1. The preceding command (in this case the rm) never ran because of an earlier &&
    2. The preceding command ran and returned nonzero

    OP’s code relies on the 1 case, but wrongly assumed that the 2 case will never happen. That’s the footgun.

    • TwilightKiddy@scribe.disroot.org
      link
      fedilink
      English
      arrow-up
      4
      ·
      edit-2
      7 hours ago

      || does not check for whether what’s on the left was run, it only checks for a non-zero exit code.

      In this case the left operand for || is &&, not the rm command. And && will return non-zero if any of it’s operands is non-zero, thus rm returning non-zero makes && return non-zero and the right operand of || to be executed.

      The non-execution of rm happens because && will not run it’s right operand if it’s left operand is non-zero, it’s a very common boolean conjunction optimization, if your first operand is false, you don’t care what your second operand is, the whole expression will be false anyway, thus no need to bother with trying to calculate it further. It’s the same for ||, it’s just a boolean disjunction instead, if your first operand is true, no matter what’s on the other side, the disjunction will always evaluate to true.