A collection of pure bash alternatives to external
processes.
The goal of this book is to document commonly-known and lesser-known methods of doing various tasks using only built-in bash features. Using the snippets from this bible can help remove unneeded dependencies from scripts and in most cases make them faster. I came across these tips and discovered a few while developing neofetch, pxltrm and other smaller projects.
The snippets below are linted using shellcheck and tests have been written where applicable. Want to contribute? Read the CONTRIBUTING.md. It outlines how the unit tests work and what is required when adding snippets to the bible.
See something incorrectly described, buggy or outright wrong? Open an issue or send a pull request. If the bible is missing something, open an issue and a solution will be found.
This book is also available to purchase on leanpub. https://leanpub.com/bash
A collection of pure bash alternatives to external processes and programs. The bash scripting language is more powerful than people realise and most tasks can be accomplished without depending on external programs.
Calling an external process in bash is expensive and excessive use will cause a noticeable slowdown. Scripts and programs written using built-in methods (where applicable) will be faster, require fewer dependencies and afford a better understanding of the language itself.
The contents of this book provide a reference for solving problems encountered when writing programs and scripts in bash. Examples are in function formats showcasing how to incorporate these solutions into code.
STRINGS
Trim leading and trailing white-space from string
This is an alternative to sed, awk, perl and other tools. The
function below works by finding all leading and trailing white-space and
removing it from the start and end of the string. The : built-in is used in place of a temporary variable.
$ trim_string " Hello, World "
Hello, World
$ name=" John Black "
$ trim_string "$name"
John Black
Trim all white-space from string and truncate spaces
This is an alternative to sed, awk, perl and other tools. The
function below works by abusing word splitting to create a new string
without leading/trailing white-space and with truncated spaces.
Example Function:
# shellcheck disable=SC2086,SC2048
trim_all() {
# Usage: trim_all " example string "
set -f
set -- $*
printf '%s\n' "$*"
set +f
}
Example Usage:
$ trim_all " Hello, World "
Hello, World
$ name=" John Black is my name. "
$ trim_all "$name"
John Black is my name.
Use regex on a string
The result of bash‘s regex matching can be used to replace sed for a
large number of use-cases.
CAVEAT: This is one of the few platform dependent bash features.
bash will use whatever regex engine is installed on the user’s system.
Stick to POSIX regex features if aiming for compatibility.
CAVEAT: This example only prints the first matching group. When using
multiple capture groups some modification is needed.
NEW: pure sh bible (📖 A collection of pure POSIX sh alternatives to external processes).
pure bash bible
A collection of pure bash alternatives to external processes.
The goal of this book is to document commonly-known and lesser-known methods of doing various tasks using only built-in
bashfeatures. Using the snippets from this bible can help remove unneeded dependencies from scripts and in most cases make them faster. I came across these tips and discovered a few while developing neofetch, pxltrm and other smaller projects.The snippets below are linted using
shellcheckand tests have been written where applicable. Want to contribute? Read the CONTRIBUTING.md. It outlines how the unit tests work and what is required when adding snippets to the bible.See something incorrectly described, buggy or outright wrong? Open an issue or send a pull request. If the bible is missing something, open an issue and a solution will be found.
This book is also available to purchase on leanpub. https://leanpub.com/bash
Or you can buy me a coffee.
Table of Contents
bashbinarybashprocessforloop syntaxifsyntaxcasestatement to set variablereadas an alternative to thesleepcommandstrftimeFOREWORD
A collection of pure
bashalternatives to external processes and programs. Thebashscripting language is more powerful than people realise and most tasks can be accomplished without depending on external programs.Calling an external process in
bashis expensive and excessive use will cause a noticeable slowdown. Scripts and programs written using built-in methods (where applicable) will be faster, require fewer dependencies and afford a better understanding of the language itself.The contents of this book provide a reference for solving problems encountered when writing programs and scripts in
bash. Examples are in function formats showcasing how to incorporate these solutions into code.STRINGS
Trim leading and trailing white-space from string
This is an alternative to
sed,awk,perland other tools. The function below works by finding all leading and trailing white-space and removing it from the start and end of the string. The:built-in is used in place of a temporary variable.Example Function:
Example Usage:
Trim all white-space from string and truncate spaces
This is an alternative to
sed,awk,perland other tools. The function below works by abusing word splitting to create a new string without leading/trailing white-space and with truncated spaces.Example Function:
Example Usage:
Use regex on a string
The result of
bash‘s regex matching can be used to replacesedfor a large number of use-cases.CAVEAT: This is one of the few platform dependent
bashfeatures.bashwill use whatever regex engine is installed on the user’s system. Stick to POSIX regex features if aiming for compatibility.CAVEAT: This example only prints the first matching group. When using multiple capture groups some modification is needed.
Example Function:
Example Usage: