expr

From NetBSD Wiki

Jump to: navigation, search

With expr(1) you can perform simple calculations in shell scripts.

Examples

Be sure to escape special characters for the shell.

There's addition:

 $ expr 1 + 2
 3

You can use a logical or like in C:

 $ expr 0 \| 1
 1

But it also works on strings:

 $ expr "" \| b
 b

You can also compare values:

 $ expr 1 \> 2
 0

The program also gives an exit status of 1 in this case so you can use it with test (be careful! the exit code is the inverse of the displayed value)

In the context of a shell script, you could do this to create a "for loop":

 i=0
 while [ $i -lt 10 ]; do
    echo $i
    i=`expr $i + 1`
 done

This will print the numbers from 0 to 9 on the screen.

See also

View source code (Please report any bugs or suggestions here).

Retrieved from "http://wiki.netbsd.se/expr"
Personal tools