3
#!/bin/bash
# query1.sh

numbers=(53 8 12 9 784 69)
echo ${numbers[3]} # <--- this echoes "9" to standard output.

number=numbers[3]
echo number # <--- this echoes "number" to stdout.
echo $number # <--- this echoes "numbers[3]" to stdout.
echo ... <--- ???

What syntax should I use to echo the variable named number and get "9" as the standard output?

3

That's where you want variable indirection using the ${!var} operator:

bash-4.4$ numbers=(53 8 12 9 784 69)
bash-4.4$ number=numbers[3]
bash-4.4$ echo "${!number}"
9

The zsh equivalent would be:

numbers=(53 8 12 9 784 69)
number=numbers[4]
echo ${(P)number}

(zsh arrays indices start at 1 like in most other shells and shell tools, so you can use numbers[4] to get the 4th number).

If you wanted to assign the value of 4th element of the array to $number, you'd do number=$numbers[4] in zsh, or number=${numbers[3]} in ksh (and bash which just copied ksh arrays).

  • Sure, it can be done by using indirection, but I wouldn't go as far as to say that it's what the OP wants. It seems more reasonable to expect that what they want is the correct syntax to get the element 9 directly, whereas this is more like a workaround for their incorrect guess. – Tom Fenech Sep 18 '17 at 19:10
  • @Tom, that would be the last part of the answer. But it looks like they known already how to get the value of an element of the array (echo ${numbers[3]} in the question). – Stéphane Chazelas Sep 18 '17 at 19:27
  • Yeah, good point. My understanding was that they hadn't worked out that they could assign ${numbers[3]} to another variable. – Tom Fenech Sep 18 '17 at 19:31
  • @Stéphane: Many thanks. Further to this: What if the array's index is a variable (let's say $i, where i=3)? The syntax has to change again.. – Anthony Webber Sep 18 '17 at 20:12
  • @AnthonyWebber, the array index is taken as an arithmetic expression, so you can use numbers[i]. – Stéphane Chazelas Sep 18 '17 at 20:17
6

Your problem is that this:

number=numbers[3]

Is not assigning the 4th element of the array numbers to the variable number. That's because numbers[3] doesn't actually mean anything:

$ numbers=(53 8 12 9 784 69)
$ echo $numbers[3]
53[3]

What happens is that when you use the name of the array alone as a variable, what is actually being used is the first element of the array (53). So $numbers[3] becomes 53[3]. What you want is:

$ number=${numbers[3]}
$ echo $number
9
0

When you declare a variable, you don't need to use the $, but when you use it, you should.

So just assign like this, and it should works number=${numbers[3]}

  • That won't and doesn't work, and isn't what I asked. But I appreciate you answering. – Anthony Webber Sep 18 '17 at 14:08
  • Yep, i made a mistake in the syntax. I corrected it. – Carpette Sep 18 '17 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.