{"id":13770643,"url":"https://github.com/pirate/bash-utils","last_synced_at":"2025-03-26T20:30:40.901Z","repository":{"id":54470335,"uuid":"202754204","full_name":"pirate/bash-utils","owner":"pirate","description":"A collection of hand-crafted bash scripts for various common tasks.","archived":false,"fork":false,"pushed_at":"2024-11-04T00:09:44.000Z","size":133,"stargazers_count":236,"open_issues_count":0,"forks_count":24,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-22T09:08:23.125Z","etag":null,"topics":["bash","bash-library","bash-scripting","cloudflare","dns","dotfiles","letsencrypt","shell-scripts","tools","unix"],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pirate.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-16T15:36:36.000Z","updated_at":"2025-03-11T12:29:45.000Z","dependencies_parsed_at":"2022-08-13T16:40:47.524Z","dependency_job_id":"adae35d0-5cf6-43f4-adc1-ebabef704a31","html_url":"https://github.com/pirate/bash-utils","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pirate%2Fbash-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pirate%2Fbash-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pirate%2Fbash-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pirate%2Fbash-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pirate","download_url":"https://codeload.github.com/pirate/bash-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245731171,"owners_count":20663132,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","bash-library","bash-scripting","cloudflare","dns","dotfiles","letsencrypt","shell-scripts","tools","unix"],"created_at":"2024-08-03T17:00:40.157Z","updated_at":"2025-03-26T20:30:40.886Z","avatar_url":"https://github.com/pirate.png","language":"Shell","readme":"# bash-utils\n\nA collection of my hand-crafted bash scripts and helper functions for various common tasks.\n\n## Code Layout\n\n- `bin/` is a collection of finished scripts, for doing everything you need to do related to a specific task (e.g. `dns` can both set and fetch DNS values from a variety of providers)\n- `lib/` is a collection of adapters to interact with 3rd party tools or scripts, e.g. cloudflare/letsencrypt/etc\n- `util/` is a collection of pure bash functions to make development in bash easier e.g. logging/configuration/error handling/etc.\n\n\n## Reading List\n\nFor a list of my favorite CLI utilities for Linux/macOS, and much more, see here: \n\nhttps://docs.sweeting.me/s/system-monitoring-tools ⭐️\n\nFor my **Fish shell** functions, snippets, and reading list see here:  \n\nhttps://github.com/pirate/fish-functions\n\n### Manpages and CLI Explainer Tools\n\n- https://linux.die.net/man/\n- https://wiki.tilde.fun/admin/linux/cli/start\n- https://explainshell.com/ ([Github](https://github.com/idank/explainshell))\n- https://tldr.sh/\n- http://bropages.org/\n- https://regex101.com/\n\n### Articles, Tools, and More\n\n - ShellCheck: life-changing BASH linter and testing toolkit              https://github.com/koalaman/shellcheck\n - How to do things safely in bash                                        https://github.com/anordal/shellharden/blob/master/how_to_do_things_safely_in_bash.md\n - 30 interesting commands for the Linux shell – Víctor López Ferrando    https://www.lopezferrando.com/30-interesting-shell-commands/\n - 7 Surprising Bash Variables                                            https://zwischenzugs.com/2019/05/11/seven-surprising-bash-variables/\n - anordal/shellharden                                                    https://github.com/anordal/shellharden/blob/master/how_to_do_things_safely_in_bash.md\n - barryclark/bashstrap                                                   https://github.com/barryclark/bashstrap\n - BashPitfalls : Greg's Wiki                                             http://mywiki.wooledge.org/BashPitfalls\n - Common shell script mistakes                                           http://www.pixelbeat.org/programming/shell_script_mistakes.html\n - Comparison of all the UNIX shells                                      http://hyperpolyglot.org/unix-shells\n - Defensive Bash Programming                                             https://www.kfirlavi.com/blog/2012/11/14/defensive-bash-programming/index.html or https://jonlabelle.com/snippets/view/markdown/defensive-bash-programming\n - Bash FAQ and Cookbook                                                  https://mywiki.wooledge.org/BashFAQ\n - Detecting the use of \"curl | bash\" server side                         https://idontplaydarts.com/2016/04/detecting-curl-pipe-bash-server-side\n - Gensokyo Blog - Use Bash Builtins shell,fish,bash                      https://blog.gensokyo.io/a/fafbe742.html\n - Rich’s sh (POSIX shell) tricks                                         http://www.etalabs.net/sh_tricks.html\n - Shell Scripts Matter                                                   https://dev.to/thiht/shell-scripts-matter\n - Shell Style Guide                                                      https://google.github.io/styleguide/shell.xml\n - Shellcode Injection - Dhaval Kapil                                     https://dhavalkapil.com/blogs/Shellcode-Injection/\n - Something you didn't know about functions in bash                      http://catonmat.net/blog/bash-functions\n - Ten More Things I Wish I’d Known About bash                            https://zwischenzugs.com/2018/01/21/ten-more-things-i-wish-id-known-about-bash\n - Ten Things I Wish I’d Known About bash                                 https://zwischenzugs.com/2018/01/06/ten-things-i-wish-id-known-about-bash\n - Testing Bash scripts with BATS                                         https://opensource.com/article/19/2/testing-bash-bats\n - Testing Bash scripts with Critic.sh                                    https://github.com/Checksum/critic.sh\n - Useful BASH and UNIX commands                                          [https://cb.vu/unixtoolbox.xhtml](https://web.archive.org/web/20210916210855/http://cb.vu/unixtoolbox.xhtml)\n - When Bash Scripts Bite :: Jane Street Tech Blogs                       https://blogs.janestreet.com/when-bash-scripts-bite/\n - Bashible: Ansible-like framework for bash-based devops                 https://github.com/mig1984/bashible\n - Auto-parse help text from comment at the top of script                 https://samizdat.dev/help-message-for-shell-scripts/\n - Make bash scripts safer by writing them in Rust                        https://github.com/rust-shell-script/rust_cmd_lib\n - Additional shell options for non-trivial bash scripts                  https://saveriomiroddi.github.io/Additional-shell-options-for-non-trivial-bash-shell-scripts/\n - Bash unit testing framework                                            https://github.com/pgrange/bash_unit\n - What exactly was the point of `[ “x$var” = “xval” ]`?                  https://www.vidarholen.net/contents/blog/?p=1035\n - How to Write Indempotent Bash Scripts                                  https://arslan.io/2019/07/03/how-to-write-idempotent-bash-scripts/\n - Better Bash Scripting in 15 Minutes                                    http://robertmuth.blogspot.com/2012/08/better-bash-scripting-in-15-minutes.html\n - Argbash: Argument parsing toolkit                                      https://github.com/matejak/argbash\n - Bash Exit Traps: Towards Safer Bash Scripts                            http://redsymbol.net/articles/bash-exit-traps/\n - Advanced Bash Scripting Guide by Mendel Cooper                         https://hangar118.sdf.org/p/bash-scripting-guide/\n - Bash Debugging Zine by Julia Evans                                     https://wizardzines.com/comics/bash-debugging/\n - Bash Process Backgrounding and Daemon Management                       https://mywiki.wooledge.org/ProcessManagement\n - The Art of Command Line                                                https://github.com/jlevy/the-art-of-command-line\n - Basn $Namerefs                                                         https://rednafi.com/misc/bash_namerefs/\n\nIf any of these links are down, see https://archive.sweeting.me or https://archive.org for mirrors.\n\n---\n\n## Useful Helper Commands\n\n#### Unofficial Bash Strict Mode\n\n```bash\n#!/usr/bin/env bash\n\n### Bash Environment Setup\n# http://redsymbol.net/articles/unofficial-bash-strict-mode/\n# https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html\n# set -o xtrace\n# set -x\n# shopt -s nullglob\nset -o errexit\nset -o errtrace\nset -o nounset\nset -o pipefail\nIFS=$'\\n'\n\nSCRIPT_DIR=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" \u003e/dev/null 2\u003e\u00261 \u0026\u0026 pwd )\"\n```\n\n#### `timeout`\n\ntimeout executes the ssh command (with args) and sends a SIGTERM if ssh doesn't return after 5 second. for more details about timeout, read this document： http://man7.org/linux/man-pages/man1/timeout.1.html\n\n```bash\ntimeout 5 some-slow-command\n\n# or on mac:\nbrew install coreutils\ngtimeout 5 some-slow-command\n```\n\n#### `nohup`\n\n#### `expect`\n\n#### `trap`\n\n#### `getopts` / `argbash`\n\n```bash\n# parse and handle passed CLI arguments sanely\nwhile getopts \":mnopq:rs\" Option\ndo\n  case $Option in\n    m     ) echo \"Scenario #1: option -m-   [OPTIND=${OPTIND}]\";;\n    n | o ) echo \"Scenario #2: option -$Option-   [OPTIND=${OPTIND}]\";;\n    p     ) echo \"Scenario #3: option -p-   [OPTIND=${OPTIND}]\";;\n    q     ) echo \"Scenario #4: option -q-\\\n                  with argument \\\"$OPTARG\\\"   [OPTIND=${OPTIND}]\";;\n    #  Note that option 'q' must have an associated argument,\n    #+ otherwise it falls through to the default.\n    r | s ) echo \"Scenario #5: option -$Option-\";;\n    *     ) echo \"Unimplemented option chosen.\";;   # Default.\n  esac\ndone\n```\n\n#### `trap`\n\n```bash\n#!/bin/bash\nscratch=$(mktemp -d -t tmp.XXXXXXXXXX)\nfunction finish {\n  rm -rf \"$scratch\"\n}\ntrap finish EXIT\n```\n\n#### `pkill`\n\n```bash\n# kill any processes matching given regex\npkill nginx\n```\n\n#### `eval`\n\n```bash\na='$b'\nb='$c'\nc=d\n\necho $a             # $b\n                    # First level.\neval echo $a        # $c\n                    # Second level.\neval eval echo $a   # d\n                    # Third level.\n```\n\n#### `exec`\n\n```bash\n# This shell builtin replaces the current process with a specified command\n# useful for when the last command in a script is a long running process you want to kick off, and you dont want it to be a child of bash\n\nexec some-daemon-that-runs-forever\n```\n\n#### `dpkg -s \u003cpkgname\u003e` / `dpkg --compare-versions \"20.04.2\" \"ge\" \"18.04.12\"`\n\ncheck pkg info of any installed package, and compare semver/date/incremental versions easily\n\n#### `pipeexec`\n\nHave total control over piping between processes, including doing crazy things like piping a processes own stdout into its stdin, launching complex directed graphs of pipes as a single process, etc.\n\nhttps://github.com/flonatel/pipexec\n\n\n### `perl -pE`: Best simple find-and-replace regex\n\n```bash\necho '0.0.0.0:443-\u003e443/tcp' | perl -pE 's/0.0.0.0:(\\d+)-\u003e.*/$1/gm'    # 443\n```\n\n#### `sed`: Truncate strings with `...` ellipsis\n\n```bash\n# Example: Truncate if longer than 15 characters\necho \"short string\"     | sed 's/\\(.\\{15\\}\\).*/\\1.../'       # short string\necho \"some long string\" | sed 's/\\(.\\{15\\}\\).*/\\1.../'       # some long st...\n\n# Bonus: get terminal width in columns\nTERMINAL_WIDTH=$(tput cols)\n```\n","funding_links":[],"categories":["Vim (text editor)"],"sub_categories":["Offline usage \u003ca href=\"https://github.com/chubin/cheat.sh#installation\"\u003einstall\u003c/a\u003e cheat script manually\u003cbr\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpirate%2Fbash-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpirate%2Fbash-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpirate%2Fbash-utils/lists"}