i ve this shell script ; it's a loop which set in the variable "a" each time result :
declare -a names=("one" "two" "three" "four")
for item in "${names[@]}";
do
a="$(cat <<-EOF
{
"NAME": "${item}_ABC",
"CHANGED": "${item}_CHANGING",
"VERSION": "${item}_GC",
}
EOF
)"
done
echo $a
My Purpose is how to change "a" by a dynamic variable name which be $item_MYPREFIX
(concatination :$item + _MYPREFIX )
So that my code would be generic , something like this :
for item in "${names[@]}";
do
$item_MYPREFIX="$(cat <<-EOF
{
"NAME": "${item}_ABC",
"CHANGED": "${item}_CHANGING",
"VERSION": "${item}_GC",
}
EOF
)"
done
and i would be able to display each variable : echo $one_MYPREFIX , echo $two_MYPREFIX ....
Of course it's not alerady working
Suggestions , to crrect it ?
awith a multiline string. as in:a=" {\n "NAME": "${item}..."(here, I use \n to indicate a newline that I cannot embed in the comment, rather than a literal\nto be expanded. Although you could also doa=$'{\n "NAME": "'"${item}_ABC"$'....)catwith something likejq, that's reasonable. But this usage ofcatis useless.a=$(jq -n --arg x "$item" '{NAME: "\($x)_ABC", CHANGED: "\($x)_CHANGING", VERSION: "\($x)_GC"}'); this ensures that the value ofitemis properly escaped, if necessary, to generate valid JSON.