How to Use EXPR to Perform Algebra in Unix
EXPR is a Unix utility that can be used to evaluate algebraic expressions. Use EXPR on the command line, or more powerfully by incorporating EXPR into shell scripts. Be careful to use only integer arguments to your algebraic expressions. EXPR gives an error message when presented with non-integer arguments.
Instructions
-
-
1
Type "X=11" (without the quotation marks), then click "enter" at the command prompt, and "Y=12," then click "enter" at the next command prompt. This establishes two variables, X and Y, and their values, 11 and 12. Enter "echo $X" and "echo $Y" to confirm the value of the variables.
-
2
Type "expr $X + $Y" at the command prompt. EXPR will evaluate this expression and return the value of X + Y, or 23.
-
-
3
Try subtraction, multiplication and division by typing expr "$X - $Y," "expr $X \* $Y," and "expr $X / $Y." (The symbol for multiplication in EXPR must be typed "\*" to avoid the special meaning of "*" in the shell as a wildcard for any character any number of times.) The answers are what you would expect, except in the case of division, in which EXPR gives the answer as 0. Since EXPR only works with integers, it rounds the decimal answer down to the nearest integer.
-
4
Perform a series of calculations by entering the variables as an array, then using EXPR to iterate over the values. To square all integers from one to 10, you would enter "sq=( 1 2 3 4 5 6 7 8 9 10 ); for sq in ${sq[@]}; do expr $sq \* $sq; done." There are four commands here, separated by semicolons. In order, they create an array called sq that contains the integers one through 10; call each value of sq separately; square each value of sq once called; and terminate the program.
-
5
Use a shell script to pass any arguments that you want to EXPR. To evaluate the quadratic expression 2x^2 - 5x + 3 for any integer value, write the following shell script in a text editor, save it as expr_test, and make it executable by typing "chmod 755 expr_test" at the command prompt.
*****************************************************************************
#!/bin/sh
for var in "$@"
do expr 2 \* $var \* $var - 5 \* $var + 3
done
*****************************************************************************
Now you can pass integer values to be evaluated at the command prompt. Typing "./expr_test 1 3 5 11" causes EXPR to evaluate the quadratic expression for one, three, five, and 11. Note that EXPR follows the correct order of operations in evaluating the quadratic expression.
-
1
Tips & Warnings
Use the scripts in this how-to as models to iterate over any algebraic expression that you can create with expr.
******************************************************************************
There are other tools for evaluating algebraic expressions at the command line and through shell scripts in unix, including the utilities bc and dc. These tools are sometimes more powerful than EXPR; for example, they are able to handle non-integer values.
References
- Photo Credit Jupiterimages/Photos.com/Getty Images