I looked at bash man page and the [[
says it uses Conditional Expressions. Then I looked at Conditional Expressions section and it lists the same operators as test
(and [
).
So I wonder, what is the difference between [
and [[
in Bash?
There is no difference if you do not put $* or $@ in quotes. But if you put them inside quotes (which you should, as a general good practice), then $@ will pass your parameters as separate parameters, whereas $* will just pass all params as a single parameter.
Double Brackets i.e. [[]] is an enhanced (or extension) version of standard POSIX version, this is supported by bash and other shells(zsh,ksh). In bash, for numeric comparison we use eq , ne , lt and gt , with double brackets for comparison we can use == , != , <, and > literally. [ is a synonym for test command.
So basically, $# is a number of arguments given when your script was executed. $* is a string containing all arguments. For example, $1 is the first argument and so on. This is useful, if you want to access a specific argument in your script.
[[
is bash's improvement to the [
command. It has several enhancements that make it a better choice if you write scripts that target bash. My favorites are:
It is a syntactical feature of the shell, so it has some special behavior that [
doesn't have. You no longer have to quote variables like mad because [[
handles empty strings and strings with whitespace more intuitively. For example, with [
you have to write
if [ -f "$file" ]
to correctly handle empty strings or file names with spaces in them. With [[
the quotes are unnecessary:
if [[ -f $file ]]
Because it is a syntactical feature, it lets you use &&
and ||
operators for boolean tests and <
and >
for string comparisons. [
cannot do this because it is a regular command and &&
, ||
, <
, and >
are not passed to regular commands as command-line arguments.
It has a wonderful =~
operator for doing regular expression matches. With [
you might write
if [ "$answer" = y -o "$answer" = yes ]
With [[
you can write this as
if [[ $answer =~ ^y(es)?$ ]]
It even lets you access the captured groups which it stores in BASH_REMATCH
. For instance, ${BASH_REMATCH[1]}
would be "es" if you typed a full "yes" above.
You get pattern matching aka globbing for free. Maybe you're less strict about how to type yes. Maybe you're okay if the user types y-anything. Got you covered:
if [[ $ANSWER = y* ]]
Keep in mind that it is a bash extension, so if you are writing sh-compatible scripts then you need to stick with [
. Make sure you have the #!/bin/bash
shebang line for your script if you use double brackets.
[
is the same as the test
builtin, and works like the test
binary (man test) [
in all the other sh-based shells in many UNIX-like environments&&
and ||
operators must be in separate brackets. !
outside the first bracket to use the shell's facility for inverting command return values.==
and !=
are literal string comparisons[[
is a bash ==
and !=
apply bash pattern matching rules, see "Pattern Matching" in man bash
=~
regex match operator!
, &&
, and ||
logical operators within the brackets to combine subexpressionsAside from that, they're pretty similar -- most individual tests work identically between them, things only get interesting when you need to combine different tests with logical AND/OR/NOT operations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With