Skip to main content
1 of 6
Premraj
  • 2.7k
  • 2
  • 27
  • 28

#Grouping Commands

Bash provides two ways to group a list of commands to be executed as a unit.

( list ) Placing a list of commands between parentheses causes a subshell environment to be created , and each of the commands in list to be executed in that subshell. Since the list is executed in a subshell, variable assignments do not remain in effect after the subshell completes.

$ a=1; (a=2; echo "inside: a=$a"); echo "outside: a=$a"
inside: a=2
outside: a=1

{ list; } Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. The semicolon (or newline) following list is required. Source

${} Parameter expansion Ex:  ANIMAL=duck; echo One $ANIMAL, two ${ANIMAL}s
$() Command substitution Ex: result=$(COMMAND) 
$(()) Arithmetic expansion Ex: var=$(( 20 + 5 )) 

#Conditional Constructs

Single Bracket i.e. []
For comparison ==, !=, <, and > and should be used and for numeric comparison eq, ne,lt and gt should be used.

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.

For comparison ==, !=, <, and > can use literally.

  • [ is a synonym for test command. Even if it is built in to the shell it creates a new process.
  • [[ is a new improved version of it, which is a keyword, not a program.
  • [[ is understood by Korn and Bash.

Source

Premraj
  • 2.7k
  • 2
  • 27
  • 28