Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

expr

From Wikipedia, the free encyclopedia
expr
Example ofexpr command that does basic summation
Original authorsDick Haight
(AT&T Bell Laboratories)
DevelopersVariousopen-source andcommercial developers
Initial release1979; 47 years ago (1979)
Operating systemUnix,Unix-like,IBM i
PlatformCross-platform
TypeCommand

expr is ashellcommand that evaluates anexpression and outputs the result. It evaluatesinteger orstring expressions, including pattern matchingregular 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 forUnix v7 and standardized byPOSIX, the command is available onIBM i,[1] and forWindows viaUnxUtils.[2][3]

Example

[edit]

The following is a (non-POSIX-compliant) example involvingBoolean 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 theor true, so 1 is the result. The programexit status is zero for this example.

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

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

since it accepts the expression as a single argument.

Portability

[edit]

Forportable 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

[edit]

References

[edit]
  1. ^IBM."IBM System i Version 7.2 Programming Qshell"(PDF).IBM.Archived(PDF) from the original on 2020-09-18. Retrieved2020-09-05.
  2. ^"Port of the most important GNU utilities to Windows".Archived from the original on 2022-09-07. Retrieved2026-02-03.
  3. ^"Native Win32 ports of some GNU utilities".unxutils.sourceforge.net.Archived from the original on 2006-02-09. Retrieved2026-02-03.

External links

[edit]
The WikibookGuide to Unix has a page on the topic of:Commands
File system
Processes
User environment
Text processing
Shell builtins
Searching
Documentation
Software development
Miscellaneous
File system
Text utilities
Shell utilities
Stub icon

ThisUnix-related article is astub. You can help Wikipedia byadding missing information.

Retrieved from "https://en.wikipedia.org/w/index.php?title=Expr&oldid=1336334870"
Categories:
Hidden category:

[8]ページ先頭

©2009-2026 Movatter.jp