tldr/pages/common/expr.md

25 lines
746 B
Markdown
Raw Normal View History

# expr
> Evaluate expressions and manipulate strings.
2021-03-30 14:54:01 +01:00
> More information: <https://www.gnu.org/software/coreutils/manual/html_node/expr-invocation.html>.
- Get string length:
`expr length {{string}}`
2016-04-21 13:48:00 +01:00
- Evaluate logical or math expression with an operator ('+', '-', '*', '&', '|', etc.). Special symbols should be escaped:
2016-04-21 13:48:00 +01:00
`expr {{first_argument}} {{operator}} {{second_argument}}`
- Get position of the first character in 'string' that matches 'substring':
`echo $(expr index {{string}} {{substring}})`
- Extract part of the string:
`echo $(expr substr {{string}} {{position_to_start}} {{number_of_characters}}`
- Extract part of the string which matches a regular expression:
`echo $(expr {{string}} : '\({{regular_expression}}\)')`