Skip to content

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] LAST
seq [OPTIONS] FIRST LAST
seq [OPTIONS] FIRST INCREMENT LAST
FlagDescription
-s SEP, --separator=SEPUse SEP as the separator between numbers (outputs a single BashText string instead of individual objects)
-w, --equal-widthPad numbers with leading zeros so all have equal width

Without -s, each result is a PsBash.SeqOutput object:

PropertyTypeDescription
Valuelong or doubleThe numeric value of the sequence element
IndexintZero-based position in the sequence
BashTextstringFormatted string representation (includes padding when -w is used)

Count from 1 to 5:

Terminal window
seq 5
1
2
3
4
5

Each line is a typed SeqOutput object:

Terminal window
$nums = seq 5
$nums[2].Value # 3
$nums[2].Index # 2

Specify a start and end:

Terminal window
seq 3 7
3
4
5
6
7

Custom increment:

Terminal window
seq 0 2 10
0
2
4
6
8
10

Countdown with negative increment:

Terminal window
seq 5 -1 1
5
4
3
2
1

Comma-separated output:

Terminal window
seq -s ', ' 1 5
1, 2, 3, 4, 5

Equal-width zero padding:

Terminal window
seq -w 1 100
001
002
003
...
099
100

Decimal sequences:

Terminal window
seq 0.0 0.5 2.0
0.0
0.5
1.0
1.5
2.0

Pipeline composition — types survive through the pipeline:

Terminal window
seq 1 10 | grep '[13579]$'
1
3
5
7
9

Evaluate arithmetic, comparison, or string expressions and return the result.

expr EXPRESSION

Expressions use infix notation for arithmetic and comparisons, and keyword-prefix notation for string operations.

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Integer division (truncates toward zero)
%Modulus (remainder)
OperatorDescription
=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.

SyntaxDescription
expr length STRINGLength of the string
expr substr STRING POS LENSubstring starting at 1-based POS for LEN characters
expr index STRING CHARS1-based position of the first character in CHARS found in STRING, or 0 if none
expr match STRING REGEXMatch STRING against a POSIX BRE anchored at start; returns the captured group if \(...\) is present, otherwise the match length

Each result is a PsBash.ExprOutput object:

PropertyTypeDescription
Valuelong or stringThe result as a number (when the result is numeric) or string
BashTextstringString representation of the result

Basic arithmetic:

Terminal window
expr 2 + 3
5

The result is a typed object:

Terminal window
$r = expr 10 '*' 5
$r.Value # 50

Integer division and modulus:

Terminal window
expr 17 / 5
3
Terminal window
expr 17 '%' 5
2

Comparison:

Terminal window
expr 10 '>' 5
1
Terminal window
expr 3 '=' 3
1

String length:

Terminal window
expr length 'hello world'
11

Substring extraction:

Terminal window
expr substr 'hello world' 7 5
world

Character index:

Terminal window
expr index 'hello' 'lo'
3

Returns 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:

Terminal window
expr match 'archive-2024.tar.gz' '.*-\([0-9]*\)'
2024

The \(...\) capture group extracts the year. Without a capture group, match returns the length of the matched portion.

Single operand passthrough:

Terminal window
expr 'hello'
hello

When given a single operand, expr echoes it as the result.

seqexpr
PurposeGenerate number sequencesEvaluate expressions
Output typeSeqOutput per numberSingle ExprOutput
Decimal supportYesInteger arithmetic only
String operationsNolength, substr, index, match
Pipeline useGenerates multiple objects for pipingReturns a single result