# problem with expr command

**URL:** <https://community.unix.com/t/problem-with-expr-command/167540>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [December 10, 2006, 9:40am UTC](https://community.unix.com/t/problem-with-expr-command/167540 "2006-12-10T09:40:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ravi\_raj\_kumar](https://community.unix.com/letter_avatar/ravi_raj_kumar/32/6_22734026db12803ebb3e059622ac3534.png) [@ravi\_raj\_kumar](https://community.unix.com/u/ravi_raj_kumar)\
**Post date:** [December 10, 2006, 9:40am UTC](https://community.unix.com/t/problem-with-expr-command/167540/1 "2006-12-10T09:40:42Z")

</div>

🙂  
hi Unix gurus,  
Pls consider the following piece of code  
str='hello'  
length=echo $str|wc -c  
echo $length  
y= ` expr \( 80 - $length \) `  
echo $y  
😕  
The last echo stmt is displaying 0 as the result.  
If i put direct value like 6 instead of $length in i 3rd stmt it is giving the correct result.  
I came to know that $length is not getting accessed inside of expr command.

any help pls.

cheers  
Ravi Raj Kumar

---

<div class="post-metadata">

**Author:** ![tayyabq8](https://community.unix.com/user_avatar/community.unix.com/tayyabq8/32/483_2.png) [@tayyabq8](https://community.unix.com/u/tayyabq8)\
**Post date:** [December 10, 2006, 10:30am UTC](https://community.unix.com/t/problem-with-expr-command/167540/2 "2006-12-10T10:30:24Z")

</div>

You are not assigning value to variable length correctly, try using backquotes there as well

```nohighlight
length=`echo $str|wc -c`

```

---

<div class="post-metadata">

**Author:** ![ripat](https://community.unix.com/letter_avatar/ripat/32/6_22734026db12803ebb3e059622ac3534.png) [@ripat](https://community.unix.com/u/ripat)\
**Post date:** [December 10, 2006, 3:09pm UTC](https://community.unix.com/t/problem-with-expr-command/167540/3 "2006-12-10T15:09:22Z")

</div>

Another way of doing it (bash)

```nohighlight
str='hello'
length=${#str}
y=$(( 80 - $length ))

```

---

<div class="post-metadata">

**Author:** ![ravi\_raj\_kumar](https://community.unix.com/letter_avatar/ravi_raj_kumar/32/6_22734026db12803ebb3e059622ac3534.png) [@ravi\_raj\_kumar](https://community.unix.com/u/ravi_raj_kumar)\
**Post date:** [December 11, 2006, 12:59am UTC](https://community.unix.com/t/problem-with-expr-command/167540/4 "2006-12-11T00:59:36Z")

</div>

🙂  
hi Ripat,  
Thank u for ur reply.The method u suggested is working fine.  
But i want it to be worked in the following way.

str='hello'  
length=`echo $str|wc -c`  
echo $length  
y= ` expr \( 80 - $length \) `  
echo $y

cheers  
Ravi Raj Kumar

---

<div class="post-metadata">

**Author:** ![cskumar](https://community.unix.com/letter_avatar/cskumar/32/6_22734026db12803ebb3e059622ac3534.png) [@cskumar](https://community.unix.com/u/cskumar)\
**Post date:** [December 11, 2006, 2:06am UTC](https://community.unix.com/t/problem-with-expr-command/167540/5 "2006-12-11T02:06:32Z")

</div>

str='hello'  
length=`echo $str|wc -c`  
echo $length  
y=`expr 80 - $length`  
echo $y

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [December 11, 2006, 2:07am UTC](https://community.unix.com/t/problem-with-expr-command/167540/6 "2006-12-11T02:07:44Z")

</div>

try this

```nohighlight
y=`expr 23 - $length`

```

---

<div class="post-metadata">

**Author:** ![ravi\_raj\_kumar](https://community.unix.com/letter_avatar/ravi_raj_kumar/32/6_22734026db12803ebb3e059622ac3534.png) [@ravi\_raj\_kumar](https://community.unix.com/u/ravi_raj_kumar)\
**Post date:** [December 11, 2006, 2:18am UTC](https://community.unix.com/t/problem-with-expr-command/167540/7 "2006-12-11T02:18:09Z")

</div>

🙂

Fine the above code is working.  
But i wonder that ,why its not giving result,if we put parantheses.  
we must to put parantheses in arithmetc expressions na.  
suppose we have a long expression like 10 + 20 -30 + 40 \*2 ,then will the above technique works 😕

any help pls

cheers  
Ravi Raj Kumar 🙂

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [December 11, 2006, 2:50am UTC](https://community.unix.com/t/problem-with-expr-command/167540/8 "2006-12-11T02:50:00Z")

</div>

```nohighlight
expr 10 + 20 - 30 + \( 40 \* 2 \)

```

This works if you give a space between operator and operand

```nohighlight
y=`expr \( 23 - $len \)`

```

---

<div class="post-metadata">

**Author:** ![fazliturk](https://community.unix.com/letter_avatar/fazliturk/32/6_22734026db12803ebb3e059622ac3534.png) [@fazliturk](https://community.unix.com/u/fazliturk)\
**Post date:** [September 3, 2007, 4:59am UTC](https://community.unix.com/t/problem-with-expr-command/167540/9 "2007-09-03T04:59:07Z")

</div>

you can also use let

let " y=(80-$x)\*10/2+$c " etc. between " " just aritmetical operations no ekstra things.
