Loading repository dataβ¦
Loading repository dataβ¦
dylanaraps / repository
π 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.
bash binarybash processread as an alternative to the sleep commandstrftimeA 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.
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.
Example Function:
trim_string() {
# Usage: trim_string " example string "
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
Example Usage:
$ trim_string " Hello, World "
Hello, World
$ name=" John Black "
$ trim_string "$name"
John Black
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