Jay Taylor's notesback to listing index
What is the difference between double and single square brackets in bash?[web search]
There are several differences. In my opinion, a few of the most important are:
For the most part, the rest is just some nicer syntax. To see more differences, I recommend this link to a FAQ answer: What is the difference between test, [ and [[ ?. In fact, if you are serious about bash scripting, I recommend reading the entire wiki, including the FAQ, Pitfalls, and Guide. The test section from the guide section explains these differences as well, and why the author(s) think
Keywords: Keywords are quite like builtins, but the main difference is that special parsing rules apply to them. For example, [ is a bash builtin, while [[ is a bash keyword. They are both used for testing stuff, but since [[ is a keyword rather than a builtin, it benefits from a few special parsing rules which make it a lot easier:
The first example returns an error because bash tries to redirect the file b to the command [ a ]. The second example actually does what you expect it to. The character < no longer has its special meaning of File Redirection operator.
Some differences on Bash 4.3.11:
Recommendation: always use
There are POSIX equivalents for every
If you use
Based on a quick reading of the relevant sections of the manpage, the primary difference appears to be that the
Single Bracket i.e.
Enhanced Brackets i.e.
In all the above examples, we used only single brackets to enclose the conditional expression, but bash allows double brackets which serves as an enhanced version of the single-bracket syntax.
Not the answer you're looking for? Browse other questions tagged linux unix bash scripting shell or ask your own question.
6 years ago
198 People Chatting
The Comms Room4 hours ago - Jenny D
Hot Network Questions
- About us: why do websites show the CEO, founders, and employees on this page?
- UNION ALL or other ways to return first row of result-set
- Aside from flavor, is there any benefit to a Monk striking multiple targets?
- Can I remove the lens on a Minolta Hi Matic 9 to add a zoom lens?
- Why don't researchers request payment for refereeing?
- How to obtain this expression in terms of real functions
- Boolean search explained
- Cylinder leak down test to detect oil leakage
- How do I say that I am interested in a lot of things without coming off either as superficial or arrogant
- What does RTC stand for?
- If we threw a baseball from the ISS, could we deorbit the ball?
- What are Mahavakyas?
- Am I allowed to show properly redacted exams to a colleague to determine if cheating occurred?
- Do we know where Ridley Scott got the idea for the xenomorph inner-mouth?
- Quali sono i verbi di seconda coniugazione che non terminano in "-ere"?
- In a formal paper, should I censor "brainf**k", the name of a programming language?
- Manager asks if a coworker can stay at my house
- Warming up (first)
- What does the first number mean on a wand?
- Mint is becoming more withered
- Why are there no guns in Alien³?
- I got an email threatening to DDOS me if I don't pay a ransom. What should I do?
- Has there ever been an application of dividing by zero?
- What's the smallest change to physics required to allow magic?
|Technology||Life / Arts||Culture / Recreation||Science||Other|