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?
Saturday, 30 December 2017
bash - How to use double or single brackets, parentheses, curly braces
Answer
In Bash,
test
and [
are shell
builtins.
The href="http://mywiki.wooledge.org/BashFAQ/031" rel="noreferrer">double
bracket, which is a shell keyword, enables additional functionality. For
example, you can use &&
and ||
instead of -a
and -o
and there's a
regular expression matching operator
=~
.
Also, in a simple
test, double square brackets seem to evaluate quite a lot quicker than single
ones.
$ time for ((i=0; i<10000000; i++)); do [[ "$i"
= 1000 ]]; done
real 0m24.548s
user
0m24.337s
sys 0m0.036s
$ time for ((i=0; i<10000000;
i++)); do [ "$i" = 1000 ]; done
real 0m33.478s
user
0m33.478s
sys
0m0.000s
The braces,
in addition to delimiting a variable name are used for href="http://tiswww.case.edu/php/chet/bash/bashref.html#Brace-Expansion"
rel="noreferrer">parameter expansion so you can do things
like:
Truncate
the contents of a variable$
var="abcde"; echo
${var%d*}
abcMake
substitutions similar to
sed
$
var="abcde"; echo
${var/de/12}
abc12Use
a default value$ default="hello";
unset var; echo
${var:-$default}
helloand
several
more
Also,
brace expansions create lists of strings which are typically iterated over in
loops:
$ echo
f{oo,ee,a}d
food feed fad
$ mv
error.log{,.OLD}
(error.log is renamed to error.log.OLD because the brace
expression
expands to "mv error.log error.log.OLD")
$ for
num in {000..2}; do echo "$num";
done
000
001
002
$ echo
{00..8..2}
00 02 04 06 08
$ echo {D..T..4}
D H
L P T
Note
that the leading zero and increment features weren't available before Bash
4.
Thanks to gboffi for reminding me about brace
expansions.
Double parentheses are used for
rel="noreferrer">arithmetic
operations:
((a++))
((meaning
= 42))
for ((i=0; i<10;
i++))
echo $((a + b + (14 *
c)))
and they enable
you to omit the dollar signs on integer and array variables and include spaces around
operators for readability.
Single brackets are
also used for rel="noreferrer">array
indices:
array[4]="hello"
element=${array[index]}
Curly
brace are required for (most/all?) array references on the right hand
side.
ephemient's
comment reminded me that parentheses are also used for subshells. And that they are used
to create arrays.
array=(1 2
3)
echo
${array[1]}
2
php - file_get_contents shows unexpected output while reading a file
I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...
-
I have an app which needs a login and a registration with SQLite. I have the database and a user can login and register. But i would like th...
-
I got an error in my Java program. I think this happens because of the constructor is not intialized properly. My Base class Program public ...
-
I would like to use enhanced REP MOVSB (ERMSB) to get a high bandwidth for a custom memcpy . ERMSB was introduced with the Ivy Bridge micro...
No comments:
Post a Comment