Fundamentals 4 min read

Unlock Powerful Bash Scripting with the Pure Bash Bible – Key Snippets & Tricks

Discover how the open‑source “Pure Bash Bible” compiles essential Bash snippets—ranging from trimming whitespace and regex matching to array deduplication—providing fast, dependency‑free solutions, complete with example commands and a GitHub repository that has amassed over 24 000 stars.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Unlock Powerful Bash Scripting with the Pure Bash Bible – Key Snippets & Tricks

Pure Bash Bible Overview

The open‑source "Pure Bash Bible" gathers a comprehensive set of Bash code snippets that let you perform common tasks without external dependencies, making scripts faster and more portable.

Key Functions

Trim whitespace

trim_string() {
    # Usage: trim_string "   example   string    "
    : "${1#"${1%%[![:space:]]*}"}"
    : "${_%"${_##*[![:space:]]}"}"
    printf %s "$_"
}

Example usage:

$ trim_string "    Hello,  World    "
Hello,  World
$ name="   John Black  "
$ trim_string "$name"
John Black

Regex matching

regex() {
    # Usage: regex "string" "regex"
    [[ $1 =~ $2 ]] && printf %s "${BASH_REMATCH[1]}"
}

Example usage:

$ regex    hello  ^s*(.*)
hello
$ regex "#FFFFFF" ^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$
#FFFFFF
$ regex "red" ^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$
# no output

Array deduplication

remove_array_dups() {
    # Usage: remove_array_dups "array"
    declare -A tmp_array
    for i in "$@"; do
        [[ $i ]] && IFS=" " tmp_array["${i:- }"]=1
    done
    printf %s "${!tmp_array[@]}"
}

Example usage:

$ remove_array_dups 1 1 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5
1
2
3
4
5
$ arr=(red red green blue blue)
$ remove_array_dups "${arr[@]}"
red
green
blue

The full collection of snippets is hosted on GitHub at https://github.com/dylanaraps/pure-bash-bible, where the project has gathered over 24 000 stars and 2 000 forks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

open sourceGitHubBashcode snippetsShell scripting
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.