# Variable Initialize

**URL:** <https://community.unix.com/t/variable-initialize/155221>\
**Category:** Shell Programming and Scripting\
**Created:** [August 6, 2005, 11:16pm UTC](https://community.unix.com/t/variable-initialize/155221 "2005-08-06T23:16:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolbudy](https://community.unix.com/letter_avatar/coolbudy/32/6_449c533f586939b6e5f246632a46421f.png) [@coolbudy](https://community.unix.com/u/coolbudy)\
**Post date:** [August 6, 2005, 11:16pm UTC](https://community.unix.com/t/variable-initialize/155221/1 "2005-08-06T23:16:09Z")

</div>

Hi, i m trying to loop a variable value, Basically i m reading variable value from an input file and using in rest of the program. Bu my problem is when i first time read value and assign to variable it works fine, but in second read it concatinate second value to first value with a LF. I want to use one value at a time. Here is what i am doing

carrier\_id=`cat ${cnt_path}mpd_rxsh_carrier_id.dat | head -${i}`  
Value::  
1st read  
253090  
2nd read  
253090  
253029  
3rd read  
253090  
253029  
255055

my input file mpd\_rxsh\_carrier\_id.dat is like  
253090  
253029  
255055

Please let me know how i can initialize a variable to hold only new value or there is any other way to do this.

thanks

---

<div class="post-metadata">

**Author:** ![blowtorch](https://community.unix.com/user_avatar/community.unix.com/blowtorch/32/136_2.png) [@blowtorch](https://community.unix.com/u/blowtorch)\
**Post date:** [August 7, 2005, 1:40pm UTC](https://community.unix.com/t/variable-initialize/155221/2 "2005-08-07T13:40:33Z")

</div>

> [@coolbudy](#):
>
> Hi, i m trying to loop a variable value, Basically i m reading variable value from an input file and using in rest of the program. Bu my problem is when i first time read value and assign to variable it works fine, but in second read it concatinate second value to first value with a LF. I want to use one value at a time. Here is what i am doing
> 
> carrier\_id=`cat ${cnt_path}mpd_rxsh_carrier_id.dat | head -${i}`  
> Value::  
> 1st read  
> 253090  
> 2nd read  
> 253090  
> 253029  
> 3rd read  
> 253090  
> 253029  
> 255055

Your error is quite obvious. Your loop variable is 'i', and as the value of i increases, head -${i} returns more and more lines. Let me explain.

At your first pass through the loop, the value of i is 1. You get the first line. So far so good. At the second pass the value of i becomes 2 and head -${i} becomes head -2, which returns the first two lines.. this will continue as long as the value of i increases.

If you want to read the value of carrier id from the file into variable carrier\_id, you have to use

```plaintext
while read carrier_id;do
..
# do your stuff here
..
done < ${cnt_path}mpd_rxsh_carrier_id.dat

```

---

<div class="post-metadata">

**Author:** ![coolbudy](https://community.unix.com/letter_avatar/coolbudy/32/6_449c533f586939b6e5f246632a46421f.png) [@coolbudy](https://community.unix.com/u/coolbudy)\
**Post date:** [August 7, 2005, 3:04pm UTC](https://community.unix.com/t/variable-initialize/155221/3 "2005-08-07T15:04:15Z")

</div>

Thanks Buddy...It works...After posting my query i realize tht head is not meant for it...

Thanks for giving the best solution.
