2023-07-05 11:43:35 +02:00
|
|
|
# Grouping commands
|
|
|
|
|
|
|
|
## Synopsis
|
|
|
|
|
|
|
|
{ <LIST>; }
|
|
|
|
|
|
|
|
{
|
|
|
|
<LIST>
|
|
|
|
}
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
2024-01-29 02:07:56 +01:00
|
|
|
The [list](../../syntax/basicgrammar.md#lists) `<LIST>` is simply executed in
|
2023-07-05 11:43:35 +02:00
|
|
|
the **current** shell environment. The list must be terminated with a
|
|
|
|
**newline** or **semicolon**. For parsing reasons, the curly braces must
|
2024-10-08 06:00:17 +02:00
|
|
|
be separated from `<LIST>` by a **semicolon** and **blanks** if they're
|
2023-07-05 11:43:35 +02:00
|
|
|
in the same line! [^1][^2]
|
|
|
|
|
|
|
|
This is known as a **group command**. The return status is the [exit
|
2024-01-29 02:07:56 +01:00
|
|
|
status (exit code)](../../scripting/basics.md#exit_codes) of the list.
|
2023-07-05 11:43:35 +02:00
|
|
|
|
|
|
|
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
|
2024-01-29 02:07:56 +01:00
|
|
|
definition](../../syntax/basicgrammar.md#shell_function_definitions), though not
|
2024-03-30 19:22:45 +01:00
|
|
|
the only compound command that's valid there:
|
2023-07-05 11:43:35 +02:00
|
|
|
|
|
|
|
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
|
2024-04-01 06:10:32 +02:00
|
|
|
semicolon or a newline must separate the enclosed list. -- thanks
|
2023-07-05 11:43:35 +02:00
|
|
|
`geirha` at Freenode
|
|
|
|
|
|
|
|
[^2]: The main reason is the fact that in shell grammar, the curly
|
2024-04-01 06:10:32 +02:00
|
|
|
braces are not control operators but reserved words -- TheBonsai
|