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.
Author(s): Dylan Araps
Edition: 1
Year: 2018
Language: English
Pages: 40
Pure Bash Bible
Dylan Araps
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 Comparisons
Variable Conditionals
Variable Comparisons
ARITHMETIC OPERATORS
Assignment
Arithmetic
Bitwise
Logical
Miscellaneous
ARITHMETIC
Simpler syntax to set variables
Ternary Tests
TRAPS
Do something on script exit
Ignore terminal interrupt (CTRL+C, SIGINT)
React to window resize
Do something before every command
Do something when a shell function or a sourced file finishes executing
PERFORMANCE
Disable Unicode
OBSOLETE SYNTAX
Shebang
Command Substitution
Function Declaration
INTERNAL VARIABLES
Get the location to the bash binary
Get the version of the current running bash process
Open the user's preferred text editor
Get the name of the current function
Get the host-name of the system
Get the architecture of the Operating System
Get the name of the Operating System / Kernel
Get the current working directory
Get the number of seconds the script has been running
Get a pseudorandom integer
INFORMATION ABOUT THE TERMINAL
Get the terminal size in lines and columns (from a script)
Get the terminal size in pixels
Get the current cursor position
CONVERSION
Convert a hex color to RGB
Convert an RGB color to hex
CODE GOLF
Shorter for loop syntax
Shorter infinite loops
Shorter function declaration
Shorter if syntax
Simpler case statement to set variable
OTHER
Use read as an alternative to the sleep command
Check if a program is in the user's PATH
Get the current date using strftime
Get the username of the current user
Generate a UUID V4
Progress bars
Get the list of functions in a script
Bypass shell aliases
Bypass shell functions
Run a command in the background
AFTERWORD