Shell - Compound Commands

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)

1 - About

A compound command is one of the following:

Advertising

2.1 - (list)

(list)

where

  • () defines the execution environment as a subshell
    • Variable assignments and builtin commands that affect the shell’s environment do not remain in effect after the command completes.
    • The return status is the exit status of list.
  • list is a list

2.2 - {list}

{ list; }

where:

  • {} defines a group command that is simply executed in the current shell environment.
  • - Since '{}' do not cause a word break, '{}' must be separated from list by whitespace.
  • list is a list
  • ;: a semicolon is the terminator (or a newline)
  • The return status is the exit status of list.
Advertising

2.3 - ((expression))

((expression))

where:

  • The expression is evaluated according to the rules described under ARITHMETIC EVALUATION.
  • If the value of the expression is:
    • non-zero, the return status is 0;
    • otherwise the return status is 1.

This is exactly equivalent to/alias to let

let "expression"

2.4 - [[ expression ]]

2.5 - To continue

      When  the	 == and != operators are used, the string to the right
      of the operator is considered a pattern and matched according to
      the  rules described below under Pattern Matching.  If the shell
      option nocasematch is enabled, the match	is  performed  without
      regard  to  the case of alphabetic characters.  The return value
      is 0 if the string matches (==) or does not match (!=) the  pat-
      tern, and 1 otherwise.  Any part of the pattern may be quoted to
      force it to be matched as a string.
      An additional binary operator, =~, is available, with  the  same
      precedence  as  ==  and  !=.  When it is used, the string to the
      right of the operator is considered an extended regular  expres-
      sion and matched accordingly (as in regex(3)).  The return value
      is 0 if the string matches the pattern, and 1 otherwise.	If the
      regular  expression  is syntactically incorrect, the conditional
      expression’s return value is 2.  If the shell option nocasematch
      is enabled, the match is performed without regard to the case of
      alphabetic  characters.	Substrings  matched  by	 parenthesized
      subexpressions  within  the  regular expression are saved in the
      array variable BASH_REMATCH.  The element of  BASH_REMATCH  with
      index 0 is the portion of the string matching the entire regular
      expression.  The element of BASH_REMATCH with  index  n  is  the
      portion  of the string matching the nth parenthesized subexpres-
      sion.
      Expressions may  be  combined  using  the	 following  operators,
      listed in decreasing order of precedence:
      ( expression )
	     Returns  the  value  of  expression.  This may be used to
	     override the normal precedence of operators.
      ! expression
	     True if expression is false.
      expression1 && expression2
	     True if both expression1 and expression2 are true.
      expression1 || expression2
	     True if either expression1 or expression2 is true.
      The && and || operators do not evaluate expression2 if the value
      of  expression1  is  sufficient to determine the return value of
      the entire conditional expression.
     for name [ in word ] ; do list ; done
      The list of words following in is expanded, generating a list of
      items.  The variable name is set to each element of this list in
      turn, and list is executed each time.  If the in word  is	 omit-
      ted,  the	 for  command  executes	 list once for each positional
      parameter that is set (see PARAMETERS below).  The return status
      is  the  exit  status of the last command that executes.	If the
      expansion of the items following in results in an empty list, no
      commands are executed, and the return status is 0.
     for (( expr1 ; expr2 ; expr3 )) ; do list ; done
      First, the arithmetic expression expr1 is evaluated according to
      the rules described  below  under	 ARITHMETIC  EVALUATION.   The
      arithmetic  expression  expr2 is then evaluated repeatedly until
      it evaluates to zero.  Each time expr2 evaluates to  a  non-zero
      value,  list  is executed and the arithmetic expression expr3 is
      evaluated.  If any expression is omitted, it behaves  as	if  it
      evaluates to 1.  The return value is the exit status of the last
      command in list that is executed, or false if any of the expres-
      sions is invalid.
     select name [ in word ] ; do list ; done
      The list of words following in is expanded, generating a list of
      items.  The set of expanded words is  printed  on	 the  standard
      error,  each  preceded  by a number.  If the in word is omitted,
      the positional parameters are printed  (see  PARAMETERS  below).
      The  PS3 prompt is then displayed and a line read from the stan-
      dard input.  If the line consists of a number  corresponding  to
      one  of  the  displayed  words, then the value of name is set to
      that word.  If the line is empty, the words and prompt are  dis-
      played again.  If EOF is read, the command completes.  Any other
      value read causes name to be set to  null.   The	line  read  is
      saved  in	 the  variable REPLY.  The list is executed after each
      selection until a break command is executed.  The exit status of
      select  is the exit status of the last command executed in list,
      or zero if no commands were executed.
     case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
      A case command first expands word, and tries to match it against
      each pattern in turn, using the same matching rules as for path-
      name expansion (see Pathname  Expansion  below).	 The  word  is
      expanded	using  tilde  expansion, parameter and variable expan-
      sion, arithmetic	substitution,  command	substitution,  process
      substitution  and	 quote	removal.   Each	 pattern  examined  is
      expanded using tilde expansion, parameter	 and  variable	expan-
      sion, arithmetic substitution, command substitution, and process
      substitution.  If the shell option nocasematch is	 enabled,  the
      match  is	 performed  without  regard  to the case of alphabetic
      characters.  When a match is found, the  corresponding  list  is
      executed.	  After	 the  first  match,  no subsequent matches are
      attempted.  The exit status is zero if no pattern matches.  Oth-
      erwise,  it  is  the exit status of the last command executed in
      list.
     if list; then list; [ elif list; then list; ] ... [ else list; ] fi
      The if list is executed.	If its exit status is zero,  the  then
      list  is	executed.   Otherwise,	each  elif list is executed in
      turn, and if its exit status is  zero,  the  corresponding  then
      list is executed and the command completes.  Otherwise, the else
      list is executed, if present.  The exit status is the exit  sta-
      tus of the last command executed, or zero if no condition tested
      true.
     while list; do list; done
     until list; do list; done
      The while command continuously executes the do list as  long  as
      the  last	 command  in list returns an exit status of zero.  The
      until command is identical to the while command, except that the
      test  is	negated;  the  do list is executed as long as the last
      command in list returns a non-zero exit status.  The exit status
      of  the  while and until commands is the exit status of the last
      do list command executed, or zero if none was executed.
Advertising