🐺

グループ構文`{}`を利用した出力

に公開

はじめに

GitHub Actions で Job Summary への出力をする際に一つのまとまりで出力したい場面がありグループ構文 {} を利用した話です。

実装

以下のように echo コマンドでグループ構文 {} を利用して出力します。

{
  echo "## 見出し"
  echo ""
  echo "- リスト1"
  echo "- リスト2"
} >> hoge.md

グループ構文内で複数行の出力をまとめてリダイレクトすることができます。

グループ構文

3.2.5.3 Grouping Commands
Bash provides two ways to group a list of commands to be executed as a unit. When commands are grouped, redirections may be applied to the entire command list. For example, the output of all the commands in the list may be redirected to a single stream.
https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html

グループ構文は2種類用意されているようで(){}があります。
違いとしてはサブシェル環境で実行されるかどうかです。
※ 出力のリダイレクトに関しては同じ動作をします。

  • () : サブシェル環境で実行される
  • {} : 現在のシェル環境で実行される

その他のbashに関する情報は以下です。

https://www.gnu.org/software/bash/manual/html_node/index.html

Discussion