1

As per the below outputs, look like only ZONES1 is array, the other two are string, why?

$ ZONES0=$(aws ec2 describe-availability-zones --region ap-southeast-2  --query AvailabilityZones[*].ZoneId[] --output text)

echo ${ZONES0[0]}
apse2-az3 apse2-az1 apse2-az2


$ ZONES1=(`aws ec2 describe-availability-zones --region ap-southeast-2    --query AvailabilityZones[*].ZoneId[] --output text`)

$echo ${ZONES1[0]}
$ apse2-az3

$ ZONES2=$(aws ec2 describe-availability-zones --region ap-south-1 | jq -r '.AvailabilityZones[].ZoneId')

$echo ${ZONES2[0]}
 aps1-az1 aps1-az3 aps1-az2

screenshot of the codes

If I run

ZONES0=( $(aws ec2 describe-availability-zones --region ap-southeast-2  --query AvailabilityZones[*].ZoneId[] --output text) ). 

the output is below

$ echo $ZONES0
apse2-az3
$ echo $ZONES0[0]
apse2-az3[0]

enter image description here

rach
  • 669
  • 2
  • 8
  • 31
tiger06
  • 11
  • 2

1 Answers1

0

ZONES1 is array because in bash arrays can be defined using a compound assignments of the form:

name=(value1 value2 … )

So your expression returns apse2-az3 apse2-az1 apse2-az2 which is placed in the (...) to declare an array.

If you want to do the same for ZONES0, you just place in the compound assignment:

ZONES0=( $(aws ec2 describe-availability-zones --region ap-southeast-2  --query AvailabilityZones[*].ZoneId[] --output text) )
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Thanks for your answer. To make it clear, I attached a screenshot – tiger06 Sep 03 '20 at 04:34
  • @tiger06 Hi. The backtick you marked s [command substitiution](https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html) is similar to `$(...)`. You can read more on that [here](https://stackoverflow.com/questions/9449778/what-is-the-benefit-of-using-instead-of-backticks-in-shell-scripts). – Marcin Sep 03 '20 at 04:45
  • Thanks for your answer. To make it clear, I attached a screenshot. in ZONES1, there is ` in the parentheses. What is its purpose? – tiger06 Sep 03 '20 at 04:46
  • @tiger06 Its [command subsitiotion](https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html). – Marcin Sep 03 '20 at 04:47