Bash: double or single bracket, parentheses, curly braces?

In Bash, test and are builtins. The double bracket enables additional functionality. For example, you can use && and || instead of -a and -o and there's a regular expression matching operator =~.

The braces, in addition to delimiting a variable name are used for parameter expansion so you can do things like: Truncate the contents of a variable $ var="abcde"; echo ${var%d*} abc Make substitutions similar to sed $ var="abcde"; echo ${var/de/12} abc12 Use a default value $ default="hello"; unset var; echo ${var:-$default} hello and several more Double parentheses are used for arithmetic operations: ((a++)) ((meaning = 42)) for ((i=0; iEphemient's comment reminded me that parentheses are also used for subshells. And that they are used to create arrays. Array=(1 2 3) echo ${array1} 2.

1 WARNING: That function is a fork bomb, do not run it. See: en.wikipedia.org/wiki/Fork_bomb – Dennis Williamson Feb 3 '10 at 19:37 1 It's only a fork bomb if you invoke it with an additional :. – ephemient Feb 3 '10 at 19:42.

A single bracket () usually actually calls program named ; man test or man for more info. Example: $ VARIABLE=abcdef $ if $VARIABLE == abcdef ; then echo yes ; else echo no ; fi yes The double bracket () does the same thing (basically) as a single bracket, but is a bash builtin. $ VARIABLE=abcdef $ if $VARIABLE == 123456 ; then echo yes ; else echo no ; fi no Parentheses (()) are used to create a subshell.

For example: $ pwd /home/user $ (cd /tmp; pwd) /tmp $ pwd /home/user As you can see, the subshell allowed you to perform operations without affecting the environment of the current shell. 4a. Braces ({}) are used to unambiguously identify variables.

Example: $ VARIABLE=abcdef $ echo Variable: $VARIABLE Variable: abcdef $ echo Variable: $VARIABLE123456 Variable: $ echo Variable: ${VARIABLE}123456 Variable: abcdef123456 4b. Braces are also used to execute a sequence of commands in the current shell context, e.g. $ { date; top -b -n1 | head ; } >logfile # 'date' and 'top' output are concatenated, # could be useful sometimes to hunt for a top loader ) $ { date; make 2>&1; date; } | tee logfile # now we can calculate the duration of a build from the logfile There is a subtle syntactic difference with ( ), though (see bash reference) ; essentially, a semicolon ; after the last command within braces is a must, and the braces {, } must be surrounded by spaces.

3 Well, is actually a builtin in Bash, but it is supposed to act like /bin/ as opposed to the builtin. Has different features, like more logical operations and different quoting roles. Additionally: single parentheses are also used for arrays, process substitution, and extended globs; double parentheses are used for arithmetic; curly braces {} are used for command grouping or multitudes of types of parameter expansion or brace expansion or sequence expansion.

I'm sure I've missed some other uses too... – ephemient Feb 2 '10 at 22:46 Good catch. There are a million of 'em. – Carl Norum Feb 2 '10 at 22:48.

I just wanted to add these from TLDP: ~:$ echo $SHELL /bin/bash ~:$ echo ${#SHELL} 9 ~:$ ARRAY=(one two three) ~:$ echo ${#ARRAY} 3 ~:$ echo ${TEST:-test} test ~:$ echo $TEST ~:$ export TEST=a_string ~:$ echo ${TEST:-test} a_string ~:$ echo ${TEST2:-$TEST} a_string ~:$ echo $TEST2 ~:$ echo ${TEST2:=$TEST} a_string ~:$ echo $TEST2 a_string ~:$ export STRING="thisisaverylongname" ~:$ echo ${STRING:4} isaverylongname ~:$ echo ${STRING:6:5} avery ~:$ echo ${ARRAY*} one two one three one four ~:$ echo ${ARRAY*#one} two three four ~:$ echo ${ARRAY*#t} one wo one hree one four ~:$ echo ${ARRAY*#t*} one wo one hree one four ~:$ echo ${ARRAY*##t*} one one one four ~:$ echo $STRING thisisaverylongname ~:$ echo ${STRING%name} thisisaverylong ~:$ echo ${STRING/name/string} thisisaverylongstring.

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.

Brackets if CONDITION Test construct if CONDITION Extended test construct Array1=element1 Array initialization a-z Range of characters within a Regular Expression Curly Brackets ${variable} Parameter substitution ${! Variable} Indirect variable reference { command1; command2; . .. CommandN; } Block of code {string1,string2,string3,...} Brace expansion {a..z} Extended brace expansion {} Text replacement, after find and xargs Parentheses ( command1; command2 ) Command group executed within a subshell Array=(element1 element2 element3) Array initialization result=$(COMMAND) Command substitution, new style >(COMMAND) Process substitution.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions