user6672948 user6672948 - 3 months ago 11
JSON Question

bash function input $1 is only processing first word of string & formatting issue

My BASH function:

json_format () {
echo '{
"question": "';echo "$1";echo '",'
}


for:

json_format ${questions[$Q_counter]}


Is returning:

{
"question": "
VM
",


Instead of expected json format and string:

{
"question": "VM CPU is at 100% and you'r jumpbox creds be broken! What do you do?",


The string appears to be cut off at the space after the first word "VM", and the formatting is a little off with those echo commands. How can i correct my function? Thanks!

Answer

I suggest to replace

json_format ${questions[$Q_counter]}

by

json_format "${questions[$Q_counter]}"
Comments