0

In if [ ... ], I know that [ stands for test. What does ] stands for?

Helin Wang
  • 109
  • 2
  • https://unix.stackexchange.com/a/408003/70524: "`]` is just an argument of `[` that prevents further arguments from being used." – muru Jan 05 '21 at 04:17
  • This isn't really a duplicate, it's specifically about the `]`; a couple of the answers on that other question mention in passing (correctly) that `]` is just an argument to the `[` command, but they (incorrectly) say it tells `[` where the end of the expression is. `]` can occur within the expression, but it must (also) occur at the end (i.e. as the last argument to `[`). Try `[ foo = bar` or `/usr/bin/[ foo = bar`, and you'll get an error message *from the `[` command* that "]" is missing. – Gordon Davisson Jan 05 '21 at 04:25
  • @muru As I said, that's wrong. Try `if [ ] = foo ]; then echo yes; else echo no; fi` vs `if [ ] = ] ]; then echo yes; else echo no; fi`. In both cases, `]` occurs as the first argument to `[`, but it does not prevent the other arguments from being parsed as part of the expression. – Gordon Davisson Jan 05 '21 at 04:28
  • @GordonDavisson then please update those answers. – muru Jan 05 '21 at 04:30
  • @muru I've left comments for the authors of those answers. However, I'd still argue that this is a different question that happens to overlap -- slightly -- with some of the answers to the other question. – Gordon Davisson Jan 05 '21 at 05:15
  • “I know that `[` stands for `test`” — it doesn’t, `[` and `test` are [slightly different](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html) (in particular, `[` requires `]`, `test` doesn’t, and will fail with an extra `]`). On my system they aren’t even the same binary. – Stephen Kitt Jan 05 '21 at 10:12

0 Answers0