All Projects → xwmx → Bash Boilerplate

xwmx / Bash Boilerplate

Licence: mit
A collection of Bash scripts for creating safe and useful command line programs.

Programming Languages

shell
77523 projects
bash
514 projects

Projects that are alternatives of or similar to Bash Boilerplate

Jsonf
A Unix-y utility for formatting JSON in a more stream-friendly way
Stars: ✭ 65 (-85.46%)
Mutual labels:  command-line, unix
Unix Text Commands
Unix Text Processing Command Reference
Stars: ✭ 78 (-82.55%)
Mutual labels:  command-line, unix
Ed
A modern UNIX ed (line editor) clone written in Go
Stars: ✭ 44 (-90.16%)
Mutual labels:  command-line, unix
Ipt
Interactive Pipe To: The Node.js cli interactive workflow
Stars: ✭ 783 (+75.17%)
Mutual labels:  command-line, unix
Snapstub
Copy API endpoints to your fs and run a local server using them
Stars: ✭ 193 (-56.82%)
Mutual labels:  command-line, unix
Cli Boot.camp
💻 command-line bootcamp adventure in your browser
Stars: ✭ 88 (-80.31%)
Mutual labels:  command-line, unix
Oh
A new Unix shell.
Stars: ✭ 1,206 (+169.8%)
Mutual labels:  command-line, unix
Bfs
A breadth-first version of the UNIX find command
Stars: ✭ 336 (-24.83%)
Mutual labels:  command-line, unix
Simple
The Simple Intelligent and Modular Programming Language and Environment
Stars: ✭ 120 (-73.15%)
Mutual labels:  command-line, unix
Forge Node App
🛠📦🎉 Generate Node.js boilerplate with optional libraries & tools
Stars: ✭ 90 (-79.87%)
Mutual labels:  command-line, boilerplate
Survey
A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
Stars: ✭ 2,843 (+536.02%)
Mutual labels:  command-line, unix
Jtc
JSON processing utility
Stars: ✭ 425 (-4.92%)
Mutual labels:  command-line, unix
Arduino Builder
A command line tool for compiling Arduino sketches
Stars: ✭ 417 (-6.71%)
Mutual labels:  command-line
Tads Boilerplate
Terraform + Ansible + Docker Swarm boilerplate = DevOps on 🔥🔥🔥 | Infrastructure as Code
Stars: ✭ 424 (-5.15%)
Mutual labels:  boilerplate
Rm Protection
A safe alternative for "rm".
Stars: ✭ 416 (-6.94%)
Mutual labels:  unix
Wordpress Plugin Boilerplate Powered
Wordpress Plugin Boilerplate but Powered with examples and a generator!
Stars: ✭ 413 (-7.61%)
Mutual labels:  boilerplate
N m3u8dl Cli
[.NET] m3u8 downloader 开源的命令行m3u8/HLS/dash下载器,支持普通AES-128-CBC解密,多线程,自定义请求头等. 支持简体中文,繁体中文和英文. English Supported.
Stars: ✭ 6,752 (+1410.51%)
Mutual labels:  command-line
Promptui
Interactive prompt for command-line applications
Stars: ✭ 4,621 (+933.78%)
Mutual labels:  command-line
Aquila
AquilaOS: UNIX-like Operating System
Stars: ✭ 413 (-7.61%)
Mutual labels:  unix
Yaspin
A lightweight terminal spinner for Python with safe pipes and redirects 🎁
Stars: ✭ 413 (-7.61%)
Mutual labels:  unix

Build Status

Bash Boilerplate

A collection of Bash starter scripts for easily creating safe and useful command line programs.

Bash Boilerplate is great for making standalone, portable, single-file command line programs as well as shell initialization functions. For a framework approach that's useful for task and build files, try Bask, a pure Bash mini-framework for command-centric Bash scripts.

Scripts

bash-simple

A simple bash script with some basic strictness checks and help features. Useful for simple programs that don't have many features and don't take options other than help.

Notable Features
  • Strict Mode,
  • Help template, printable with -h or --help.

bash-simple-plus

A simple bash script with some basic strictness checks, option parsing, help features, easy debug printing. Useful for regular scripts.

Notable Features
  • Strict Mode,
  • Help template, printable with -h or --help,
  • debug printing with --debug flag,
  • _exit_1 and _warn functions for error messages,
  • Option parsing.

bash-subcommands

An example of a bash program with subcommands. This contains lots of features and should be usable for creating bash programs that do multiple related tasks.

Notable Features
  • Strict Mode,
  • Help template, printable with -h or --help,
  • debug printing with --debug flag,
  • _exit_1 and _warn functions for error messages,
  • Option normalization and parsing,
  • Automatic arbitrary subcommand loading,
  • An nice, clean pattern for specifying per-subcommand help,
  • Built-in subcommands for help, version, and subcommand listing,
  • Conventions for distinguishing between functions and program subcommands,
  • Useful utility functions.

functions.bash

Shell function examples and boilerplate. The functions in this file are intended to be included in the interactive shell, which can be done by defining them in a shell init file like ~/.bashrc.

helpers.bash

Helper functions. These functions are primarily intended to be used within scripts, but can be adapted for use as shell functions.

Notes

ShellCheck

Use it. It's super useful.

ShellCheck is a static analysis and linting tool for sh/bash scripts. It's mainly focused on handling typical beginner and intermediate level syntax errors and pitfalls where the shell just gives a cryptic error message or strange behavior, but it also reports on a few more advanced issues where corner cases can cause delayed failures.

Links

It can be used with Vim via Syntastic, Emacs via Flycheck, Sublime Text 3 via SublimeLinter, VS Code via vscode-shellcheck, and Atom via linter, atom-lint, or linter-shellcheck.


Bash "Strict Mode"

These boilerplate scripts use some common settings for enforcing strictness in Bash scripts, thereby preventing some errors.

For some additional background, see Aaron Maxwell's "Unofficial Bash Strict Mode" (redsymbol.net) post.


Simple 'Strict Mode' TL;DR

Add this to the top of every script (note: an extended version of this is already included in the boilerplate scripts):

# Bash 'Strict Mode'
# http://redsymbol.net/articles/unofficial-bash-strict-mode
# https://github.com/xwmx/bash-boilerplate#bash-strict-mode
set -o nounset
set -o errexit
set -o pipefail
IFS=$'\n\t'

set -o nounset / set -u

Treat unset variables and parameters other than the special parameters @ or * as an error when performing parameter expansion. An 'unbound variable' error message will be written to the standard error, and a non-interactive shell will exit.

Usage

Short form:

set -u

Long form:

set -o nounset
Parameter Expansion

Parameter expansion can be used to test for unset variables when using set -o nounset.

http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

The two approaches that are probably the most appropriate are:

${parameter:-word}
  If parameter is unset or null, the expansion of word is substituted.
  Otherwise, the value of parameter is substituted. In other words, "word"
  acts as a default value when the value of "$parameter" is blank. If "word"
  is not present, then the default is blank (essentially an empty string).

${parameter:?word}
  If parameter is null or unset, the expansion of word (or a message to that
  effect if word is not present) is written to the standard error and the
  shell, if it is not interactive, exits. Otherwise, the value of parameter
  is substituted.
Parameter Expansion Examples

Arrays:

${some_array[@]:-}              # blank default value
${some_array[*]:-}              # blank default value
${some_array[0]:-}              # blank default value
${some_array[0]:-default_value} # default value: the string 'default_value'

Positional variables:

${1:-alternative} # default value: the string 'alternative'
${2:-}            # blank default value

With an error message:

${1:?'error message'}  # exit with 'error message' if variable is unbound

set -o errexit / set -e

Exit immediately if a pipeline returns non-zero.

Usage

Short form:

set -e

Long form:

set -o errexit
Using set -o errexit with read -rd ''

set -o errexit is super useful for avoiding scary errors, but there are some things to watch out for. When using read -rd '' with a heredoc, the exit status is non-zero, even though there isn't an error, and this setting then causes the script to exit. read -rd '' is equivalent to read -d $'\0', which means read until it finds a NUL byte, but it reaches the end of the heredoc without finding one and exits with a 1 status. Therefore, when reading from heredocs with set -e, there are three potential solutions:

Solution 1. set +e / set -e again:

set +e
read -rd '' variable <<HEREDOC
Example text.
HEREDOC
set -e

Solution 2. <<HEREDOC || true:

read -rd '' variable <<HEREDOC || true
Example text.
HEREDOC

Solution 3. Don't use set -e or set -o errexit at all.

More information:

'builtin "read -d" behaves differently after "set -e"' (lists.gnu.org)


set -o pipefail

Return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully.

Usage

Long form (no short form available):

set -o pipefail

$IFS

Set IFS to just newline and tab.

IFS=$'\n\t'

For some background, see Filenames and Pathnames in Shell: How to do it Correctly (dwheeler.com)


Misc Notes

Explicitness and clarity are generally preferable, especially since bash can be difficult to read. This leads to noisier, longer code, but should be easier to maintain. As a result, some general design preferences:

  • Use leading underscores on internal variable and function names in order to avoid name collisions. For unintentionally global variables defined without local, such as those defined outside of a function or automatically through a for loop, prefix with double underscores.
  • Always use braces when referencing variables, preferring ${NAME} instead of $NAME. Braces are only required for variable references in some cases, but the cognitive overhead involved in keeping track of which cases require braces can be reduced by simply always using them.
  • Prefer printf over echo. For more information, see: http://unix.stackexchange.com/a/65819
  • Prefer $_explicit_variable_name over names like $var.
  • Use the #!/usr/bin/env bash shebang in order to run the preferred Bash version rather than hard-coding a bash executable path.
  • Prefer splitting statements across multiple lines rather than writing one-liners.
  • Group related code into sections with large, easily scannable headers.
  • Describe behavior in comments as much as possible, assuming the reader is a programmer familiar with the shell, but not necessarily experienced writing shell scripts.

Resources

Related Projects

Examples

Scripts based on this project.

  • airport - A command line tool for Wi-Fi on macOS.
  • bask - A pure Bash mini-framework for command-centric Bash scripts.
  • bindle - A configuration and dotfile management tool for your personal unix-like computer.
  • hosts - Command line hosts file editor in a single portable script.
  • ❯ nb - CLI note-taking, bookmarking, and archiving with encryption, advanced search, Git-backed versioning and syncing, Pandoc-backed conversion, and more in a single portable script.
  • notes-app-cli - A command line interface for Notes.app on macOS.
  • pb - A tiny wrapper combining pbcopy & pbpaste in a single command.
  • search.sh - A command line search multi-tool.
  • user - Command line interface for common macOS user account operations.
  • vbox - A streamlined interface for VBoxManage, the VirtualBox command line tool.

Copyright (c) 2015 William Melody • [email protected]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].