# Bash array variables are changed in loop runtime

**URL:** <https://community.unix.com/t/bash-array-variables-are-changed-in-loop-runtime/363561>\
**Category:** UNIX for Beginners Q & A\
**Created:** [November 15, 2016, 8:59am UTC](https://community.unix.com/t/bash-array-variables-are-changed-in-loop-runtime/363561 "2016-11-15T08:59:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tmalik79](https://community.unix.com/letter_avatar/tmalik79/32/6_449c533f586939b6e5f246632a46421f.png) [@tmalik79](https://community.unix.com/u/tmalik79)\
**Post date:** [November 15, 2016, 8:59am UTC](https://community.unix.com/t/bash-array-variables-are-changed-in-loop-runtime/363561/1 "2016-11-15T08:59:43Z")

</div>

I am trying to check whether particular host and port are responding or not. I am using below script to check. but node\_port array that i am using in loop is getting replaced with previous iteration value. Script and output is given.

Please help me to understanding why node\_port values are replaced and how to avoid that.

I am using bash shell for executing this.

```plaintext
host_names=(xx.yy.zz.167 xx.yy.zz.168 xx.yy.zz.169 xx.yy.zz.170 xx.yy.zz.171 xx.yy.zz.172)
node_port=(2100 2200 2300 2400 2500)

while :
do
        LoopstartTime=$(date '+%Y-%m-%d %r %Z')
        unset i j
        for i in "${host_names[@]}"
        do
                echo "First Loop-$i"
                for j in "${node_port[@]}"
                do
                  echo " Second Loop-$j"
                  echo " variables list ${node_port[@]}"
                  echo " no of arg ${#node_port[@]}"
                  unset j
                done
        done
        LoopendTime=$(date '+%Y-%m-%d %r %Z')
        echo "Process started at ${LoopstartTime} and Completed at ${LoopendTime}"
        echo " "
        sleep $wait_time
done

```

Output is

> [@](#):
>
> First Loop-xx.yy.zz.167  
> Second Loop-2100  
> variables list 2100 2200 2300 2400 2500  
> no of arg 5  
> Second Loop-2200  
> variables list 2100 2200 2300 2400 2500  
> no of arg 5  
> Second Loop-2300  
> variables list 2200 2200 2300 2400 2500  
> no of arg 5  
> Second Loop-2400  
> variables list 2300 2200 2300 2400 2500  
> no of arg 5  
> Second Loop-2500  
> variables list 2400 2200 2300 2400 2500  
> no of arg 5

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [November 15, 2016, 9:08am UTC](https://community.unix.com/t/bash-array-variables-are-changed-in-loop-runtime/363561/2 "2016-11-15T09:08:35Z")

</div>

The output doesn't seem to belong to the script?  
And, the `node_port` array is NOT altered by that script.

---

<div class="post-metadata">

**Author:** ![tmalik79](https://community.unix.com/letter_avatar/tmalik79/32/6_449c533f586939b6e5f246632a46421f.png) [@tmalik79](https://community.unix.com/u/tmalik79)\
**Post date:** [November 16, 2016, 12:30am UTC](https://community.unix.com/t/bash-array-variables-are-changed-in-loop-runtime/363561/3 "2016-11-16T00:30:46Z")

</div>

Hi Rudic, Thanks for quick response.

Output is from above script, I just modified IP address due to sensitivity of the data. when i executed above script thats what exactly printed on screen.

Please let me know if you have any questions.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [November 16, 2016, 5:28am UTC](https://community.unix.com/t/bash-array-variables-are-changed-in-loop-runtime/363561/4 "2016-11-16T05:28:31Z")

</div>

Please DON'T edit posts after people commented on it, pulling the rug from under their feet!

Running that script gives the expected output, and none of the variables is modified. What is the error that you think you encounter?  
And, where has the

> [@](#):
>
> Fail Triggered for xx.yy.zz.167 2500 at 2016-11-15 05:45:06 AM PST

from the original post#1 gone?

---

<div class="post-metadata">

**Author:** ![tmalik79](https://community.unix.com/letter_avatar/tmalik79/32/6_449c533f586939b6e5f246632a46421f.png) [@tmalik79](https://community.unix.com/u/tmalik79)\
**Post date:** [November 16, 2016, 6:18am UTC](https://community.unix.com/t/bash-array-variables-are-changed-in-loop-runtime/363561/5 "2016-11-16T06:18:34Z")

</div>

Sorry for that.

> [@](#):
>
> Fail Triggered for xx.yy.zz.167 2500 at 2016-11-15 05:45:06 AM PST

is removed as that is not relevant.  
I am not encountering error but my array sequence is getting changed in run time.

if you observer output every time second loop is completed

```plaintext
node_port

```

variable are updated or sequence is changed.

Please go through ouput once. that has node\_port array output.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [November 16, 2016, 6:53am UTC](https://community.unix.com/t/bash-array-variables-are-changed-in-loop-runtime/363561/6 "2016-11-16T06:53:13Z")

</div>

I can't reproduce the output you posted when running the script you posted. Nor are the input arrays altered in any way.  
The idiosyncrasies mentioned must come from some parts of the script NOT posted.
