0

This is the code I have

# filename: script.sh
cluster="some_cluster_name"

aws_cli_response=$(aws ecs list-services --cluster $cluster_name --output text)
echo $aws_cli_response
echo "${aws_cli_response}"

The resulting output of sh script.sh is

SERVICEARNS <arn1> SERVICEARNS <arn2> SERVICEARNS <arn3> ...
SERVICEARNS <arn1>
SERVICEARNS <arn2>
SERVICEARNS <arn3>
...

I didn't expect "${aws_cli_response}" to somehow make the one line string into a well structured line by line result. I thought it would require some sort of looping logic to create this line by line result. But I was surprised by getting this result.

So to investigate how this result could have happened I've created a simple script that looks like this:

text="SERVICEARNS text1 SERVICEARNS text2"

echo $text
echo "${text}"

But to my surprise I got this result that I would previously have anticipated had I not seen the first script's outcome:

SERVICEARNS text1 SERVICEARNS text2
SERVICEARNS text1 SERVICEARNS text2

Could anyone explain why the first that simply echos a string variable which is just a bunch of single line text result in this clean looking string result?

Thanks in advance. Happy coding everyone.

Aesop
  • 151
  • 1
  • 7
  • 1
    The double-quotes and braces are not adding structure, the data in the variable retains the structure of the output from `aws ecs list-services` and the lack of double-quotes in the first `echo` command is *removing* it. See ["I just assigned a variable, but `echo $variable` shows something else"](https://stackoverflow.com/questions/29378566) and ["When should I double-quote a parameter expansion?"](https://stackoverflow.com/questions/55023461) (short answer: almost always). [shellcheck.net](https://www.shellcheck.net) is good at spotting problems like this; I recommend using it! – Gordon Davisson Dec 16 '22 at 18:19

0 Answers0