Intro To 'expr' Command In Linux
2024-04-18 - By Robert Elder
I use the 'expr' command to evaluate expressions:
# I use the 'expr' command to...
expr 1 + 2
This includes numeric expressions, relational expressions, and string expressions.
Numeric Expressions
# Addition
expr 1 + 2
# Subtraction
expr 10 - 3
# Multiplication
expr 2 \* 4
# Division
expr 12 / 2
# Remainder
expr 8 % 3
Relational Expressions
# Logical Or
expr 1 \| 0
# Logical And
expr 1 \& 0
# Less Than
expr 99 \< 100
# Less Than or equal to
expr 99 \<= 100
# Equal to
expr 66 = 66
# Not Equal to
expr 66 != 66
# Greater Than
expr 231 \> 99
# Greater Than or equal to
expr 231 \>= 99
String Expressions
# Return length of string:
expr length 'Hello World.'
# Return the substring at position 4 of length 9:
expr substr 'My Favourite String' 4 9
# Return the index of the first character
# in 'something' that is in the character set 'abcdef':
expr index something abcdef
# Gives length of match:
expr aaaaaa : 'a\+'
# Gives length of match:
expr match '0xF8' '0x[0-9a-fA-F][0-9a-fA-F]'
# Extracts last 4 digits of phone number:
expr '555-123-1234' : '[0-9]\{3\}-[0-9]\{3\}-\([0-9]\{4\}\)'
Use Cases For 'expr' Command
These expressions can be useful when writing shell scripts like the following:
#!/bin/bash
if [ $(expr 123 - 100) -gt 0 ]; then
echo "Do something."
else
echo "Do something else."
fi
A Warning About 'expr' Command
It can be very easy to use the 'expr' command incorrectly. For example, this greater than or equal to relationship command won't work:
expr 2 >= 1
expr: syntax error: unexpected argument ‘1’
This is because the angle bracket character has a special meaning when used on the command line, and is typically used to redirect the output of a program to a file:
man bash
...
Redirecting Output
Redirection of output causes the file whose name results from the expansion of word to be
opened for writing on file descriptor n, or the standard output (file descriptor 1) if n is
not specified. If the file does not exist it is created; if it does exist it is truncated
to zero size.
The general format for redirecting output is:
[n]>word
...
You make this expr command work correctly by escaping the angle bracket character:
expr 2 \>= 1
1
An Even Worse Problem With 'expr' Command
A similar, but even worse problem exists with multiplication expressions. If I run this expr command in an empty directory, it produces the correct output:
expr 2 * 4
8
However, if I create a single file in this directory who's name is a plus character:
touch '+'
ls -l
total 0
-rw-rw-r-- 1 robert robert 0 Apr 19 11:51 +
the exact same 'expr' command produces the wrong output:
expr 2 * 4
6
This problem is caused by a shell feature called 'globbing':
man glob
DESCRIPTION
The glob() function searches for all the pathnames matching pattern according to the rules
used by the shell (see glob(7)). No tilde expansion or parameter substitution is done; if
you want these, use wordexp(3).
and can be solved by escaping the asterisk character:
expr 2 \* 4
8
A Meme About The 'expr' Command
Given how easy it can be to mis-use the 'expr' command, I made the following meme that I am quite proud of:
The punch line is the following: If you run 'expr 4 * 1' in an empty directory, the output will be '4', but if you create a single file who's name is a plus sign character, the '*' will expand to the name of that single file (which is a plus symbol). Therefore, the expression becomes 'expr 4 + 1' which is 5.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|