0

Is there a way to show multiple elements from an array and how to show all of them using a variable for the index, for example:

Here I can get the elements :

ANIMALS=('DOG' 'CAT' 'CAW' 'BIRD')
INDEX=1
echo ${ANIMALS[$INDEX]}

CAT
INDEX=2
echo ${ANIMALS[$INDEX]}

CAW

Question 1, how can I get all elements using the variable index? I'm assigning it the value @, but I'm getting the following error:

INDEX=@
echo ${ANIMALS[$INDEX]}
-bash: @: syntax error: operand expected (error token is "@")

Expected Resutl: show all the elements in the array

Question 2, How can I show multiple elements , for example if I want to show elements in the index 0 and 3, something like:

INDEX="0  3"
echo ${ANIMALS[$INDEX]}

Expected result:

    DOG BIRD
  • 1
    Please edit your question (no comment): What have you searched for, and what did you find? What have you tried, and how did it fail? – Cyrus Apr 11 '23 at 18:23
  • 1
    In general, use `"${!array[@]}"` to get the list of keys. In your case use `INDEX=("${!ANIMALS[@]}")`. – Wiimm Apr 11 '23 at 21:45
  • `@` is a special symbol in this context. You can only pass it literally, not via a parameter expansion `$INDEX? . – user1934428 Apr 12 '23 at 06:05

2 Answers2

1

I think the best option is to use a list of numbers and iter inside it:

$ ANIMALS=('DOG' 'CAT' 'CAW' 'BIRD')
$ INDEXES=(1 3)
$ for key in "${INDEXES[@]}"; do echo "${ANIMALS[$key]}"; done

The result of this should be:

CAT
BIRD
Wiimm
  • 2,971
  • 1
  • 15
  • 25
  • `FOR` and `IN` need to be lower-case -- this isn't SQL; operators are case-sensitive here. And quoting matters. `${array[@]}` without the quotes is identical to `${array[*]}` without the quotes; you only get the additional correctness of the `@` operator when you quote `"${array[@]}"`. – Charles Duffy Apr 11 '23 at 18:30
  • @CharlesDuffy you are right, I have already changed it. Thanks – Santiago Makcimovich Apr 11 '23 at 18:31
  • Similarly, `echo ${ANIMALS[$key]}` hits the problems described in [I just assigned a variable but `echo $variable` shows something else](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) and should be `echo "${ANIMALS[$key]}"`. – Charles Duffy Apr 11 '23 at 18:31
  • 1
    I added quotes. Otherwise `${ANIMALS[@]}` will not work correctly. – Wiimm Apr 11 '23 at 21:49
0

For Question 1, you can use the "${ANIMALS[@]}" syntax to iterate through all the elements in the array:

INDEX=("${ANIMALS[@]}")
echo "${INDEX[@]}"

This will print:

DOG CAT CAW BIRD

For Question 2, if you want to show multiple elements from the array, you can specify the indices as separate elements inside curly braces "{ }" and separate them with commas ",". Here's an example:

INDEX=("0" "3")
for i in "${INDEX[@]}"; do
  echo "${ANIMALS[$i]}"
done

This will print:

DOG 
BIRD

If you want to print in the same line:

result=""
for i in "${INDEX[@]}"; do
  result+=" ${ANIMALS[$i]}"
done

# Remove leading space
result="${result# }"

echo "$result"

This will print as

DOG BIRD

Note that the elements inside curly braces "{ }" should be separated by commas "," without any spaces. Also, the indices in the array should be specified as strings, not as a single string with spaces in between.

Coder
  • 1,415
  • 2
  • 23
  • 49