bash-hackers-wiki/docs/syntax/ccmd/grouping_plain.md
Hanson Char b75c3a588b Fix hyperlinks of markdown pages at depth 3
find docs/ -depth 3  -name '*.md' | xargs grep '(.*/' -l | \
  xargs -I{} \
  sed -i '' \
  -e 's%(/\([^/#).][^/#).]*\)/\([^/#).][^/#).]*\)\(.md\)\{0\})%(../../\1/\2.md)%g' \
  -e 's%(/\([^/#).][^/#).]*\)/\([^/#).][^/#).]*\)#\([a-zA-Z_-][0-9a-zA-Z_-]*\))%(../../\1/\2.md#\3)%g' \
  -e 's%(/\([^/#).][^/#).]*\)/\([^/#).][^/#).]*\)/\([^/#).][^/#).]*\)\(.md\)\{0\})%(../../\1/\2/\3.md)%g' \
  -e 's%(/\([^/#).][^/#).]*\)/\([^/#).][^/#).]*\)/\([^/#).][^/#).]*\)#\([a-zA-Z_-][0-9a-zA-Z_-]*\))%(../../\1/\2/\3.md#\4)%g' \
  -e 's%](\([^:.>)#][^:.>)#]*\))%](../../\1.md)%g' \
  -e 's%](\([^:.>)#][^:.>)#]*\)#\([^:.>)#][^:.>)#]*\))%](../../\1.md#\2)%g' \
  {}

Related to https://github.com/flokoe/bash-hackers-wiki/issues/10
2024-01-28 17:26:57 -08:00

1.9 KiB

Grouping commands

Synopsis

{ <LIST>; }

{
<LIST>
}

Description

The list <LIST> is simply executed in the current shell environment. The list must be terminated with a newline or semicolon. For parsing reasons, the curly braces must be separated from <LIST> by a semicolon and blanks if they're in the same line! 12

This is known as a group command. The return status is the exit status (exit code) of the list.

The input and output filedescriptors are cumulative:

{
  echo "PASSWD follows"
  cat /etc/passwd
  echo
  echo "GROUPS follows"
  cat /etc/group
} >output.txt

This compound command also usually is the body of a function definition, though not the only compound command that's valid there:

print_help() {
  echo "Options:"
  echo "-h           This help text"
  echo "-f FILE      Use config file FILE"
  echo "-u USER      Run as user USER"
}

Examples

A Try-Catch block

  try_catch() {
      { # Try-block:
          eval "$@"
      } ||
      { # Catch-block:
          echo "An error occurred"
          return -1
      }
  }

Portability considerations

See also

 * [[syntax:ccmd:grouping_subshell | grouping commands in a subshell]]

  1. Actually any properly terminated compound command will work without extra separator (also in some other shells), example: { while sleep 1; do echo ZzZzzZ; done } is valid. But this is not documented, infact the documentation explicitly says that a semicolon or a newline must separate the enclosed list. -- thanks geirha at Freenode ↩︎

  2. The main reason is the fact that in shell grammar, the curly braces are not control operators but reserved words -- TheBonsai ↩︎