Skip to main content

Sequences on bash

·187 words·1 min
Orlando Gentil
Author
Orlando Gentil
Jack of all trades, master of none
Table of Contents
“You need to create a sequence of items on bash”.

TL;DR
#

Ordered by the breadth of use.

Method 01 - bash arithmetic expansion - $((expr))
#

END=5
for ((i=1;i<=END;i++)); do
    echo $i
done

prints

1
2
3
4
5

Method 02 - seq command
#

usage: seq [-w] [-f format] [-s string] [-t string] [first [incr]] last

seq 7 10

prints

7
8
9
10
seq -w 1 3 15

prints

01
04
07
10
13
seq -w -s ":" -t "\n" 1 3 15

prints

01:04:07:10:13:

Method 03 - Bracket expansion
#

Generating sequences of numbers or letters:

echo {1..5}

prints

1 2 3 4 5

echo {a..e}

prints

a b c d e

printf "%03d " {4..7}

prints (printf formatting but missing newline)

004 005 006 007 pc:/path orlando$

echo $(printf "%03d " {4..7})

prints (echo adds a newline after the output)

004 005 006 007
pc:/path orlando$

 echo {A,B}{1,2,3}

prints

A1 A2 A3 B1 B2 B3

echo file_{01..05}.backup

prints

file_01.backup file_02.backup file_03.backup file_04.backup file_05.backup

echo {one,two}_{1..3}_{a,b}

prints

one_1_a one_1_b one_2_a one_2_b one_3_a one_3_b two_1_a two_1_b two_2_a two_2_b two_3_a two_3_b

Long version
#

TBD