Wikipedia

expr

Also found in: Acronyms.
expr
Original author(s)Dick Haight
(AT&T Bell Laboratories)
Developer(s)Various open-source and commercial developers
Initial release1979
Operating systemUnix, Unix-like, IBM i
PlatformCross-platform
TypeCommand

expr is a command line utility on Unix and Unix-like operating systems which evaluates an expression and outputs the corresponding value. It first appeared in Unix v7. The expr command has also been ported to the IBM i operating system.[1]

Overview

expr evaluates integer or string expressions, including pattern matching regular expressions. Each symbol (operator, value, etc.) in the expression must be given as a separate parameter. Most of the challenge posed in writing expressions is preventing the invoking command line shell from acting on characters intended for expr to process.

Syntax

Syntax: expr expression

The operators available

  • for integers: addition, subtraction, multiplication, division and modulus
  • for strings: find regular expression, find a set of characters in a string; in some versions: find substring, length of string
  • for either: comparison (equal, not equal, less than, etc.)

Example

The following is an example involving boolean expressions:

expr length "abcdef" "<" 5 "|" 15 - 4 ">" 8 

This example outputs "1". This is because length "abcdef" is 6, which is not less than 5 (so the left side of the | returns zero). But 15 minus 4 is 11 and is greater than 8, so the right side is true, which makes the or true, so 1 is the result. The program exit status is zero for this example.

For pure arithmetic, it is often more convenient to use bc. For example:

echo "3*4+14/2" | bc 

since it accepts the expression as a single argument.

For portable shell programming use of the length and substr commands is not recommended.

See also

  • List of Unix commands

References

  1. ^ IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). Retrieved 2020-09-05.

External links

This article is copied from an article on Wikipedia® - the free encyclopedia created and edited by its online user community. The text was not checked or edited by anyone on our staff. Although the vast majority of Wikipedia® encyclopedia articles provide accurate and timely information, please do not assume the accuracy of any particular article. This article is distributed under the terms of GNU Free Documentation License.

Copyright © 2003-2025 Farlex, Inc Disclaimer
All content on this website, including dictionary, thesaurus, literature, geography, and other reference data is for informational purposes only. This information should not be considered complete, up to date, and is not intended to be used in place of a visit, consultation, or advice of a legal, medical, or any other professional.