Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • Any ideas / knowledge why doesn't -n with no operand lead to, for example, "error: -n requires an operand"? Does this question deserve to be posted as a new question?
    – pmor
    Commented Dec 28, 2023 at 11:27
  • @pmor : For reasons of coherence with bash test command I presume : See linuxcommand.org/lc3_man_pages/testh.html in particular : "Exit Status: Returns success if EXPR evaluates to true; fails if EXPR evaluates to false or an invalid argument is given."
    – MC68020
    Commented Dec 28, 2023 at 11:36
  • @pmor : In my above comment, I mean test gets a binary exit status (true exclusive-or false) not a ternary which could include some value that could be interpreted as sort of syntax error.
    – MC68020
    Commented Dec 28, 2023 at 11:57
  • 2
    @MC68020, A few test implementations (including Bash's) actually do return a distinct falsy status for an error, i.e. 0 for true, 1 for false and 2 for error. It's just that both 1 and 2 are falsy as far as shell conditionals are concerned, so one often doesn't get to see the difference. It'd need explicitly looking at the value of $?. Not that there's any invalid arguments here in this case (and anyway, [ -n ] is true), but you could try e.g. [ x x ] or similar. e.g. bash -c '[ x x ]; echo $?' prints 2.
    – ilkkachu
    Commented Dec 28, 2023 at 12:40
  • 1
    Thanks a lot @ilkkachu (since I learnt something :-) ) for that deeper insight. I updated my answer accordingly.
    – MC68020
    Commented Dec 28, 2023 at 14:53