dylanaraps /pure-bash-bible
📖 A collection of pure bash alternatives to external processes.
★ 41,716⑂ 3,657👁 8⑃ 61● 77▤ 0◈ 0
41,716
stars
+1
3,657
forks
8
watchers
61
prs
77
issues
0
releases
0
discussions
README
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 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
Or you can buy me a coffee.
Table of Contents
FOREWORD
STRINGS
Trim leading and trailing white-space from string
Trim all white-space from string and truncate spaces
Use regex on a string
Split a string on a delimiter
Change a string to lowercase
Change a string to uppercase
Reverse a string case
Trim quotes from a string
Strip all instances of pattern from string
Strip first occurrence of pattern from string
Strip pattern from start of string
Strip pattern from end of string
Percent-encode a string
Decode a percent-encoded string
Check if string contains a sub-string
Check if string starts with sub-string
Check if string ends with sub-string
ARRAYS
Reverse an array
Remove duplicate array elements
Random array element
Cycle through an array
Toggle between two values
LOOPS
Loop over a range of numbers
Loop over a variable range of numbers
Loop over an array
Loop over an array with an index
Loop over the contents of a file
Loop over files and directories
FILE HANDLING
Read a file to a string
Read a file to an array (by line)
Get the first N lines of a file
Get the last N lines of a file
Get the number of lines in a file
Count files or directories in directory
Create an empty file
Extract lines between two markers
FILE PATHS
Get the directory name of a file path
Get the base-name of a file path
VARIABLES
Assign and access a variable using a variable
Name a variable based on another variable
ESCAPE SEQUENCES
Text Colors
Text Attributes
Cursor Movement
Erasing Text
PARAMETER EXPANSION
Indirection
Replacement
Length
Expansion
Case Modification
Default Value
BRACE EXPANSION
Ranges
String Lists
CONDITIONAL EXPRESSIONS
File Conditionals
File ComparisTop issues
#76dirname is not equivalent to the utility16 comments · 2019-09-19#137[[ ' aaaa' =~ a* ]] && echo ${BASH_REMATCH[0]} || echo no8 comments · 2023-03-27#87Include prepend function8 comments · 2019-09-25#2TODO8 comments · 2018-06-14#112Shorter if syntax 7 comments · 2021-01-26#11curl/wget (need help)6 comments · 2018-06-16#110Alternative to strings4 comments · 2020-11-27#90rgb2hsl - Something fun I wrote today3 comments · 2019-10-01