0

i am searching on these forums and trying different codes, but i cant seem to get it working:

i have a variable

ip="192.168.1.227 192.168.1.205 192.168.1.217" 

no i want to split every ip address up in to different variables

ip1=192.168.1.227 ip2=192.168.1.205 ip3=192.168.1.217 

i was trying to do something with word

for ((word in $vds; i=0; i++)); do echo $word "ip$i"=$word done 

but i cant get it working.

1
  • I added the "bash" tag as your code uses bash for loop syntax.
    – Kusalananda
    CommentedDec 11, 2018 at 9:02

3 Answers 3

3

It is highly unlikely that you actually want separate variables for each individual IP number. It would be more manageable to store the IP numbers in an array.

ipnumbers=( $ip ) 

This unquoted use of $ip would (assuming the default value of the IFS variable) split its value on whitespaces (and invoke filename globbing on the resulting words, but that should not be an issue since there is no filename globbing patterns in the data). It would then assign the split up words as separate elements in the array ipnumbers.

You may then access the individual elements using e.g. "${ipnumbers[1]}" (for the second element), or loop over them with

for ipnum in "${ipnumbers[@]}"; do ...; done 
5
  • Shouldn't the FIRST element be referenced by "${ipnumbers[0]}"?
    – RudiC
    CommentedDec 11, 2018 at 10:23
  • @RudiC So it is! Thanks. I've been doing too much awk programming lately...
    – Kusalananda
    CommentedDec 11, 2018 at 10:24
  • Shouldn't you ensure that IFS is correct to make this method robust?
    – user232326
    CommentedDec 11, 2018 at 10:41
  • @Isaac I added a note about this. AFAIK, bash resets IFS when a shell starts, so if it's left unchanged it will work.
    – Kusalananda
    CommentedDec 11, 2018 at 11:02
  • Yes, sure, but you are providing an snippet (a one line part) that could be incorporated into any bigger/longer script. The note about the value of IFS is relevant and important in the this case.
    – user232326
    CommentedDec 11, 2018 at 11:19
1

You will be better off by using an array variable.

 readarray -t -d ' ' ips <<<"$ip" 

This doesn't use the "unquoted" shell splitting that is usually problematic with newlines, glob characters (*, ?, []) and what not. Then just print the array:

 printf '%s\n' "${ips[@]}" 

Or use it in a loop:

for i in "${ips[@]}"; do echo "$i" done 
    0

    While the array clearly is the approach of choice, if you really really need the individual ip$number version, try

    • eval your ip$i=$word assignment carefully looking at the quoting (with the precautions reg. the deprecated eval in mind)
    • a small loop like for word in $ip; do read ip$((++CNT)) <<< $word; done
    2
    • eval is evil. An eval is more often than not an opportunity for code injection. The value of $i (or $CNT) should be sanitized with great care. It should never contain values from an external user.
      – user232326
      CommentedDec 11, 2018 at 11:23
    • ... and it's only deprecated in the sense that its use is not generally encouraged.
      – Kusalananda
      CommentedDec 11, 2018 at 11:38

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.