expr

Example of `expr` command that does basic summation

Original author(s)| Dick Haight
(AT&T Bell Laboratories)
Developer(s)| Various open-source and commercial developers
Initial release| 1979; 47 years ago (1979)
Operating system| Unix, Unix-like, IBM i
Platform| Cross-platform
Type| Command

`expr` is a shell command that evaluates an expression and outputs the result. It evaluates integer or string expressions, including pattern matching regular expressions. Comparison operators (equal, not equal, less than, etc.) apply to both integer and string values. Integer-specific operations include addition, subtraction, multiplication, division and modulus. String-specific operators include:

* matching a regular expression (`match`, or the colon syntax  "str" : "reg" for an arbitrary string "str" and regular expression "reg")
* finding a set of characters in a string (`index`)
* finding a substring (`substr`)
* finding the length of a string (`length`)

Each symbol (operator, value, etc.) in the expression must be given as a separate parameter. A common challenge with using the command is preventing the shell from acting on input indented to be the expression as something other than expression.

Originally developed for Unix v7 and standardized by POSIX, the command is available on IBM i,[1] and for Windows via UnxUtils.[2][3]

## Contents

* 1 Example
* 1.1 Portability
* 2 See also
* 3 References
* 4 External links

## Example

The following is a (non-POSIX-compliant) 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.

### Portability

For portable shell programming, use of the "index", "length", "match" and "substr" commands must be avoided; string matching remains possible but it must use the "string : regexp" syntax.

## See also

* List of POSIX commands

## References

1. ↑ IBM. "IBM System i Version 7.2 Programming Qshell" (in en). https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc.
2. ↑ "Port of the most important GNU utilities to Windows". https://sourceforge.net/projects/unxutils/.
3. ↑ "Native Win32 ports of some GNU utilities". https://unxutils.sourceforge.net/.

## External links

| The Wikibook Guide to Unix has a page on the topic of: Commands

|

* `expr`: evaluate arguments as an expression – Commands & Utilities Reference, The Single UNIX Specification, Issue 7 from The Open Group
* expr invocation in GNU coreutils manual

* v
* t
* e

Unix command-line interface programs and shell builtins

File system|

* cat
* chmod
* chown
* chgrp
* cksum
* cmp
* cp
* dd
* du
* df
* file
* fuser
* ln
* ls
* mkdir
* mv
* pax
* pwd
* rm
* rmdir
* split
* tee
* touch
* type
* umask

Processes|

* at
* bg
* crontab
* fg
* kill
* nice
* ps
* time

User environment|

* env
* exit
* logname
* mesg
* talk
* tput
* uname
* who
* write

Text processing|

* awk
* basename
* comm
* csplit
* cut
* diff
* dirname
* ed
* ex
* fold
* head
* iconv
* join
* m4
* more
* nl
* paste
* patch
* printf
* sed
* sort
* strings
* tail
* tr
* uniq
* vi
* wc
* xargs

Shell builtins|

* alias
* cd
* echo
* test
* unset
* wait

Searching|

* find
* grep

Documentation|

* man

Software development|

* ar
* ctags
* lex
* make
* nm
* strip
* yacc

Miscellaneous|

* bc
* cal
* expr
* lp
* od
* sleep
* true and false

* Categories
* Standard Unix programs
* Unix SUS2008 utilities
* List

Template:Core Utilities commands

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Expr. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Software:Expr&oldid=4291988"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template