Math & Sequences
Math & Sequences
Section titled “Math & Sequences”seq for generating numeric sequences and expr for arithmetic and string expression evaluation.
Generate a sequence of numbers, one per line or joined with a separator.
seq [OPTIONS] LASTseq [OPTIONS] FIRST LASTseq [OPTIONS] FIRST INCREMENT LAST| Flag | Description |
|---|---|
-s SEP, --separator=SEP | Use SEP as the separator between numbers (outputs a single BashText string instead of individual objects) |
-w, --equal-width | Pad numbers with leading zeros so all have equal width |
SeqOutput Properties
Section titled “SeqOutput Properties”Without -s, each result is a PsBash.SeqOutput object:
| Property | Type | Description |
|---|---|---|
Value | long or double | The numeric value of the sequence element |
Index | int | Zero-based position in the sequence |
BashText | string | Formatted string representation (includes padding when -w is used) |
Examples
Section titled “Examples”Count from 1 to 5:
seq 512345Each line is a typed SeqOutput object:
$nums = seq 5$nums[2].Value # 3$nums[2].Index # 2Specify a start and end:
seq 3 734567Custom increment:
seq 0 2 100246810Countdown with negative increment:
seq 5 -1 154321Comma-separated output:
seq -s ', ' 1 51, 2, 3, 4, 5Equal-width zero padding:
seq -w 1 100001002003...099100Decimal sequences:
seq 0.0 0.5 2.00.00.51.01.52.0Pipeline composition — types survive through the pipeline:
seq 1 10 | grep '[13579]$'13579Evaluate arithmetic, comparison, or string expressions and return the result.
expr EXPRESSIONExpressions use infix notation for arithmetic and comparisons, and keyword-prefix notation for string operations.
Arithmetic Operators
Section titled “Arithmetic Operators”| Operator | Description |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Integer division (truncates toward zero) |
% | Modulus (remainder) |
Comparison Operators
Section titled “Comparison Operators”| Operator | Description |
|---|---|
= | Equal (case-sensitive for strings) |
!= | Not equal |
< | Less than |
<= | Less than or equal |
> | Greater than |
>= | Greater than or equal |
Comparisons return 1 for true and 0 for false. When both operands are integers, numeric comparison is used; otherwise string comparison applies.
String Operations
Section titled “String Operations”| Syntax | Description |
|---|---|
expr length STRING | Length of the string |
expr substr STRING POS LEN | Substring starting at 1-based POS for LEN characters |
expr index STRING CHARS | 1-based position of the first character in CHARS found in STRING, or 0 if none |
expr match STRING REGEX | Match STRING against a POSIX BRE anchored at start; returns the captured group if \(...\) is present, otherwise the match length |
ExprOutput Properties
Section titled “ExprOutput Properties”Each result is a PsBash.ExprOutput object:
| Property | Type | Description |
|---|---|---|
Value | long or string | The result as a number (when the result is numeric) or string |
BashText | string | String representation of the result |
Examples
Section titled “Examples”Basic arithmetic:
expr 2 + 35The result is a typed object:
$r = expr 10 '*' 5$r.Value # 50Integer division and modulus:
expr 17 / 53expr 17 '%' 52Comparison:
expr 10 '>' 51expr 3 '=' 31String length:
expr length 'hello world'11Substring extraction:
expr substr 'hello world' 7 5worldCharacter index:
expr index 'hello' 'lo'3Returns the 1-based position of the first occurrence of any character from the search string. Here l appears at position 3.
Regex match with capture group:
expr match 'archive-2024.tar.gz' '.*-\([0-9]*\)'2024The \(...\) capture group extracts the year. Without a capture group, match returns the length of the matched portion.
Single operand passthrough:
expr 'hello'helloWhen given a single operand, expr echoes it as the result.
seq vs expr
Section titled “seq vs expr”seq | expr | |
|---|---|---|
| Purpose | Generate number sequences | Evaluate expressions |
| Output type | SeqOutput per number | Single ExprOutput |
| Decimal support | Yes | Integer arithmetic only |
| String operations | No | length, substr, index, match |
| Pipeline use | Generates multiple objects for piping | Returns a single result |