Sem 3‎ > ‎OS (Unix) LAB‎ > ‎

### D5: expr

posted Oct 9, 2011, 2:36 AM by Neil Mathew   [ updated Oct 9, 2011, 5:47 AM ]
It is used to do math in the shell.

Syntax:

 `=`expr ``

Example:

 `sum=`expr \$a + \$b + \$c + \$d + \$e``

 `sum=`expr 2 + 4``

My Mistakes

1. No Space should be there between <Variable> = `

```sum= `expr \$a + \$b + \$c + \$d + \$e` X ERROR
```

`sum =`expr \$a + \$b + \$c + \$d + \$e` X ERROR`

 `sum=`expr \$a + \$b + \$c + \$d + \$e` RIGHT`

But, this is fine:

`sum=`     expr \$a + \$b + \$c + \$d + \$e` RIGHT`

2. Space must be there between the operator and operand. It is taken as string otherwise.

```sum=`echo "Enter 5 numbers: "

sum=`expr \$a+\$b+\$c+\$d+\$e`

echo "The Sum is: \$sum "```

OUTPUT:

```Enter 5 numbers:
6
7
5
4
3
The Sum is: 6+7+5+4+3```

3. Expr must contain Integer values if written without extra commands.

(Non-integer)

`area=` expr 22.0 / 7.0 \* \$r \* \$r ``

`expr: non-integer argument`

(Corrected)

`area=` expr 22 / 7 \* \$r \* \$r ``

```Enter the radius of the Circle:
1
Ans is 3```

`sum=`expr \$a + \$b + \$c + \$d + \$e` RIGHT`