Skip to content

Latest commit

 

History

History
61 lines (52 loc) · 1.48 KB

paren_bracket_brace.md

File metadata and controls

61 lines (52 loc) · 1.48 KB

parentheses, brackets, curly braces

copied from stackoverflow

  1. A single bracket [ usually actually calls a program named [; man test or man [ for more info.
VARIABLE=abcdef
if [ $VARIABLE == abcdef ] ; then 
    echo yes
else 
    echo no
fi
# yes
  1. 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
  1. Parentheses () are used to create a subshell.
pwd
/home/user 
(cd /tmp; pwd)
/tmp
pwd
/home/user

The subshell allowed performing operations without affecting the environment of the current shell.

4a. Braces {} are used to unambiguously identify variables.

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.

{ 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 ; after the last command within braces is a must, and the { , } must be surrounded by spaces.