How to use double or single brackets, parentheses, curly braces

后端 未结 7 765
心在旅途
心在旅途 2020-11-21 23:10

I am confused by the usage of brackets, parentheses, curly braces in Bash, as well as the difference between their double or single forms. Is there a clear explanation?

相关标签:
7条回答
  • 2020-11-21 23:51

    The difference between test, [ and [[ is explained in great details in the BashFAQ.

    To cut a long story short: test implements the old, portable syntax of the command. In almost all shells (the oldest Bourne shells are the exception), [ is a synonym for test (but requires a final argument of ]). Although all modern shells have built-in implementations of [, there usually still is an external executable of that name, e.g. /bin/[.

    [[ is a new improved version of it, which is a keyword, not a program. This has beneficial effects on the ease of use, as shown below. [[ is understood by KornShell and BASH (e.g. 2.03), but not by the older POSIX or BourneShell.

    And the conclusion:

    When should the new test command [[ be used, and when the old one [? If portability to the BourneShell is a concern, the old syntax should be used. If on the other hand the script requires BASH or KornShell, the new syntax is much more flexible.

    0 讨论(0)
提交回复
热议问题