0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

shell script 内でheredocを使い、json文字列を変数代入する

Last updated at Posted at 2022-11-28

heredocを受け取り、標準出力する

var=key
cat << EOS | jq
  {
    "$var": "value"
  }
EOS

出力は

{
  "key": "value"
}

heredocは文字列リテラルではなく標準入力として扱われるので、echoではなくcatの引数にする。

heredocを受け取り、変数に代入する

var=key
json=$(tr -d ' |\n' << EOS
  {
    "$var": "value"
  }
EOS
)

echo $json

出力は

> {"key":"value"}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?