I'm working on some inventory stuff and i'm trying to save all the AWS regions on one array, then, showed elements one under another to use it as an input menu.

This next command is giving me the right output but when i walk into the array with FOR, the array length is just 1 cause the result is:

aws ec2 describe-regions --output text|awk -F\t '{print $3}'| sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g'

eu-north-1 ap-south-1 eu-west-3 eu-west-2 eu-west-1 ap-northeast-2 ap-northeast-1 sa-east-1 ca-central-1 ap-southeast-1 ap-southeast-2 eu-central-1 us-east-1 us-east-2 us-west-1 us-west-2

This is how i'm filing the arrays:

# Get regions
declare -a regions=$(aws ec2 describe-regions --output text | awk -F\t '{print $3}' |  sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /')
echo -e "\nPlease, select the region you would like to query: "

# Print Regions
for (( i=0; i<$len; i++ )); do
    echo -e "$i.${regions[$i]}\n" ;
echo -e "$last All of them (this could take a while...O_o)\n"
read region_opt

if [${region_opt}!=${last}] then

What i want to have in the output is something like

  1. eu-north-1
  2. ap-south-1
  3. eu-west-3 ....

2 Answers

webb On

You're missing parentheses around your array values, e.g.,

declare -a ARRAY=(value1 value2 ... valueN)

(refs: https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html, https://www.gnu.org/software/bash/manual/bash.html)

The following forms also work, and the first (without declare -a) is given as an example in the GNU's Bash reference manual, the Bash guide for beginners, and the Advanced bash-scripting guide:

ARRAY=(value1 value2 ... valueN)
declare ARRAY=(value1 value2 ... valueN)
abdan On

$() is command substitution, is just convert any stdout to string and assign it to a variable
if you said true that the result is;
eu-north-1 ap-south-1 eu-west-3... then to get array out of it, make it syntactically appear so, then tell Bash to evaluate it as such,


after expansion it'd be the valid array syntax

 regions=(eu-north-1 ap-south-1 eu-west-3)

then would be evaluated as valid array after it's enclosed by "" and as Bash eval argument

$ eval "regions=($regions)"
$ echo ${regions[0]}

So that I am sure you will be able to accomplish and solve it on your own...