← Back to Bash Course | Chapter 4: Loops | Lesson 1 of 14

expr

In this page:

  1. expr

expr

expr is an older external command that evaluates a simple arithmetic or string expression and prints the result. It requires spaces around every operator and operand, unlike $(( )). Most modern scripts prefer $(( )) or let, but expr still appears in older scripts.

Warning: expr 1+1 (no spaces) prints 1+1 literally instead of 2 — spaces around operators are mandatory.

Example: expr

bash
#!/bin/bash
result=$(expr 5 + 3)
echo "5 + 3 = $result"

result=$(expr 10 \* 2)
echo "10 * 2 = $result"

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.