Bash Boilerplate

screenshot of Bash Boilerplate

A simple starting point for bash scripts

Overview

This article provides an analysis of a Bash boilerplate script that includes interactive mode, quiet mode, and a CLI options parser. The script also includes helper functions for printing messages and miscellaneous helpers. The interactive mode allows users to enter variables through stdin instead of the command line, making it user-friendly for occasional script usage.

Features

  • Interactive mode: Allows users to enter variables through stdin instead of the command line, providing a user-friendly alternative and simplifying the script usage.
  • Quiet mode: Suppresses script output, making it useful for running the script silently without any messages displayed.
  • CLI options parser: Supports parsing of CLI options such as -n, --name, --name=Oxy, and --name Oxy. It also supports bundling of flags, allowing combinations like -vf instead of specifying -v and -f separately.
  • Helper functions for printing messages: Provides functions like die(), out(), err(), and success(). die() outputs a message to stderr and exits with error code 1, out() outputs a message, err() outputs a message to stderr and continues running, and success() outputs a message as a string. Both success() and err() output messages with colorized symbols, as long as the script isn't piped.
  • Miscellaneous helpers: Includes functions like escape() to escape slashes in a string and confirm() to prompt the user to answer Yes or No. The confirm() function automatically returns true if the --force option is used.

Summary

This Bash boilerplate script provides several useful features for executing scripts. The interactive mode simplifies script usage by allowing users to enter variables through stdin instead of the command line. The quiet mode suppresses script output, while the CLI options parser supports various options and bundling of flags. The script also includes helper functions for printing messages and several miscellaneous helper functions. Overall, this boilerplate script offers a convenient and user-friendly approach to executing Bash scripts.