1

I crawled through lots of boards but didn't find the final solution for my problem.

I have got an array, named "array0", the name is stored to a variable called arrayname. Also I've got a logged IP address, let's say 127.0.0.1, also stored in a variable, called ip.

Now I want to assign the IP to index 3 in the array like that:

"$arrayname"[3]="$ip"    

So, this didn't work. I tried lots of ways how to solve that but none worked.
Is anyone out there who can tell me the final solution for this case?

Update: The given opportunities to handle the problem are great! But I forgot to mention that the array I'm working with is just sourced from another file (also written in bash). My goal is now to edit the array in the sourced file itself. Any more ideas for that?

5 Answers 5

5

Try

read ${arrayname}[3] <<<"$ip"
Sign up to request clarification or add additional context in comments.

7 Comments

+1 I always forget you can read into elements of an array as well.
Nice. I can't decide whether I like this read method or chepner's declare method better. But both are miles ahead of an eval-based approach. :)
@dncrft : Do you mean you want to modify the text of the sourced file itself? You should edit your new requirements into your question, or maybe start a new question.
You can't handle variables that contain newlines. Unless you do some trickery like IFS= read -r -d '' "$arrayname[3]" < <(printf '%s' "$ip"). It's not a problem here, but for someone looking for a general method, this is not as good as @PM2Ring's answer with printf.
Ok. It should be easy to edit the sourced file, probably using sed. But we'll need to see the relevant code from that file to give more details. OTOH, there may be a better way to achieve what you really want to do. So it's probably a good idea to start a new question.
|
2

You'll need to use the declare command and indirect parameter expansion, but it's a little tricky to use with array names. It helps if you think of the index as part of the variable name, instead of an operator applied to the array name, like in most languages.

array0=(1 2 3 4 5)
arrayname=array0
name=$arrayname[3]
declare "$name=$ip"
echo "${!name}

3 Comments

It is a good trick, but you do have to be sure that the contents of name are safe. Otherwise Bad Things can happen. Eg, try the above code but with name=$arrayname[3] replaced by name=$arrayname[$(ls >&2;echo 3)]. It still modifies array0 as desired, but it also has unwanted side effects. :)
Well, that's an issue with setting the value of name, not declare.
Fair enough. I should've been more explicit about that.
2

And yet another way to do it, this time using the versatile printf.

printf -v "$arrayname[3]" %s "$ip"

demo

#!/bin/bash

array0=(a b c d e)
echo "${array0[@]}"

arrayname='array0'
ip='127.0.0.1'

printf -v "$arrayname[3]" %s "$ip"
echo "${array0[@]}"

output

a b c d e
a b c 127.0.0.1 e

Comments

0

See this:

# declare -a arrayname=(element1 element2 element3)
# echo ${arrayname[0]}
  element1
# arrayname[4]="Yellow"
# echo ${arrayname[4]}
  Yellow
# export ip="192.168.190.23"
# arrayname[5]=$ip
# echo ${arrayname[5]}
  192.168.190.23

You don't have to use quotes.

1 Comment

This doesn't answer the question.
-1

After initializing the arrays, you can access the array elements using their indices as follows. Access as:

${arrayname[3]}

2 Comments

Have you understood what dncrft asked?
@Jdamian yes I just showed how to access the array element, which was wrongly accessed as shown in the example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.