AAB

Your bash cheatsheet.

AAB serves to provide you with a collection of useful reminders.


1: The basics

Shebang line

#!/usr/bin/env bash

Conditional test

[[ 1 -eq 1 ]] && echo "1 equals 1"
[[ 1 -eq 2 ]] || echo "1 does not equal 2"

Tables

$ echo -e "Name Age\nNick 28\nRobert 36\nAnne 42" | column -t

Name Age
Nick 28
Robert 36
Anne 42

Sorting

$ cat unsorted.txt
punchiest
restlessness
hummingbirds
Manitoba
equalising

$ cat unsorted.txt | sort
equalising
Manitoba
hummingbirds
restlessness
punchiest

2: Text processing

AWK


n-th Field

$ cat fields.txt
first second third fourth

$ cat fields.txt | awk '{print $2}'
second

grep


Filter for a string

$ cat filter.txt
this is
a text file

$ cat filter.txt | grep text
a text file

Filter for a string between two strings

$ cat filter.txt
this is a text file

$ cat filter.txt | grep -oP '().*?()'
a text file