# expr command

**URL:** <https://community.unix.com/t/expr-command/169524>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [March 5, 2007, 8:06am UTC](https://community.unix.com/t/expr-command/169524 "2007-03-05T08:06:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![madhu\_aqua14](https://community.unix.com/letter_avatar/madhu_aqua14/32/6_449c533f586939b6e5f246632a46421f.png) [@madhu\_aqua14](https://community.unix.com/u/madhu_aqua14)\
**Post date:** [March 5, 2007, 8:06am UTC](https://community.unix.com/t/expr-command/169524/1 "2007-03-05T08:06:26Z")

</div>

hi guys....

i hava a command expr... where i m adding a value in a loop  
like  
Tc=`expr $Tc\+ $l`  
where  
Tc is declred as a variable and every time l contains a new vaue  
if  
Tc =0 initially  
and l =2  
Tc should be equal to 0+ 2  
and then  
l = 4  
Tc = 2+4

and dispaly as 6

but after on iteration it is not adding up the value...

any reason for it

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/6_449c533f586939b6e5f246632a46421f.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [March 5, 2007, 8:19am UTC](https://community.unix.com/t/expr-command/169524/2 "2007-03-05T08:19:57Z")

</div>

> [@](#):
>
> Tc=`expr $Tc\+ $l`

why is the \+ ?

Could be just,  
Tc=`expr $Tc+ $l`

Could you please post your script if possible ?

---

<div class="post-metadata">

**Author:** ![BOFH](https://community.unix.com/letter_avatar/bofh/32/6_449c533f586939b6e5f246632a46421f.png) [@BOFH](https://community.unix.com/u/BOFH)\
**Post date:** [March 5, 2007, 8:29am UTC](https://community.unix.com/t/expr-command/169524/3 "2007-03-05T08:29:45Z")

</div>

It helps to post what answer you're getting as well. It might point to what the problem is.

Carl

---

<div class="post-metadata">

**Author:** ![madhu\_aqua14](https://community.unix.com/letter_avatar/madhu_aqua14/32/6_449c533f586939b6e5f246632a46421f.png) [@madhu\_aqua14](https://community.unix.com/u/madhu_aqua14)\
**Post date:** [March 5, 2007, 8:43am UTC](https://community.unix.com/t/expr-command/169524/4 "2007-03-05T08:43:47Z")

</div>

hi  
this is the script

for i in `ls q*.txt`  
do  
l=0  
tc=0  
var=`tail -1 $i`  
l=`echo $var | cut -d'|' -f2`  
tc=`expr $tc+$l`  
done

every time it gives the first answer  
i.e if l =2  
for the first time  
it prints 2 always  
if l = 6 next time  
tc = 8 is expected  
but displays 2 only

---

<div class="post-metadata">

**Author:** ![sb008](https://community.unix.com/letter_avatar/sb008/32/6_449c533f586939b6e5f246632a46421f.png) [@sb008](https://community.unix.com/u/sb008)\
**Post date:** [March 5, 2007, 3:16pm UTC](https://community.unix.com/t/expr-command/169524/5 "2007-03-05T15:16:49Z")

</div>

> [@madhu\_aqua14](#):
>
> hi  
> this is the script
> 
> for i in `ls q*.txt`  
> do  
> l=0  
> tc=0  
> var=`tail -1 $i`  
> l=`echo $var | cut -d'|' -f2`  
> tc=`expr $tc+$l`  
> done
> 
> every time it gives the first answer  
> i.e if l =2  
> for the first time  
> it prints 2 always  
> if l = 6 next time  
> tc = 8 is expected  
> but displays 2 only

```plaintext
tc=0
for i in `ls q*.txt`
do
  l=`tail -1 $i | cut -d'|' -f2`
  tc=`expr $tc + $l`
  echo $tc
done

```

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [March 5, 2007, 5:26pm UTC](https://community.unix.com/t/expr-command/169524/6 "2007-03-05T17:26:20Z")

</div>

> [@madhu\_aqua14](#):
>
> hi  
> this is the script
> 
> ```plaintext
> for i in `ls q*.txt`
> 
> ```

Do not do that. Not only is ls unnecessary and slow, but it will break your script if there are any spaces or other pathological characters in any filenames.

Use:

```plaintext
for i in q*.txt

```

> [@](#):
>
> ```plaintext
> do
> l=0
> tc=0
> var=`tail -1 $i`
> l=`echo $var | cut -d'|' -f2`
> tc=`expr $tc+$l`
> done
> 
> ```
> 
> every time it gives the first answer  
> i.e if l =2  
> for the first time  
> it prints 2 always  
> if l = 6 next time  
> tc = 8 is expected  
> but displays 2 only

You reset $tc to 0 on every iteration of the loop. Set it before the loop.
