0

This is my straightforward function:

generate_post_data()
{
  cat <<-EOF
  {"stringData": {}}
EOF
}

This is my other function:

generate_curl_body()
{
  template=$(generate_post_data $secret_id)
  echo "$template"
  echo "$KVS_VARIABLES" | while read -r key value
  do
    template=$(echo "$template" | jq ".stringData += { \"$key\" : \"$value\" }")
    echo $template
  done
}

The output is:

#Before while -> {"stringData": {}}
#1 iteration -> { "stringData": { "VAR1": "VAL1" } }
#2 iteration -> { "stringData": { "VAR1": "VAL1", "VAR2": "VAL2" } }
#3 iteration -> { "stringData": { "VAR1": "VAL1", "VAR2": "VAL2", "VAR3": "VAL3" } } 
#After while -> {"stringData": {}}

Why template variable is not filled?

Jordi
  • 20,868
  • 39
  • 149
  • 333
  • Possible duplicate of [Left side of pipe is the subshell?](https://stackoverflow.com/questions/5760640/left-side-of-pipe-is-the-subshell#5760832) – Anthony Geoghegan Jul 02 '18 at 10:43
  • 1
    Possible duplicate of [A variable modified inside a while loop is not remembered](https://stackoverflow.com/questions/16854280/a-variable-modified-inside-a-while-loop-is-not-remembered) – Biffen Jul 02 '18 at 10:47

1 Answers1

0

It is not filled because the | while is executed in subshell. From the manual page of Bash (for example):

Each command in a pipeline is executed as a separate process (i.e., in a subshell).

If the shell is Bash, the easy fix is to replace the echo "$KVS_VARIABLES" | with a here-string:

while read -r key value; do
  template=$(echo "$template" | jq ".stringData += { \"$key\" : \"$value\" }")
  echo $template
done <<< "$KVS_VARIABLES" 

If your shell does not accept here-strings, you can always wrap the while in a compound command { while ... done; echo "$template"; } and use an extra command substitution:

template="$(
  echo "$KVS_VARIABLES" |
  {
    while read -r key value; do
      template=$(
        echo "$template" |
        jq ".stringData += { \"$key\" : \"$value\" }"
      )
    done
    echo "$template"
  }
)"
AlexP
  • 4,370
  • 15
  • 15
  • I'm using `!/usr/bin/env sh`. I'm getting this error message: `Syntax error: redirection unexpected` – Jordi Jul 02 '18 at 10:53