Bash
It's very useful to use ShellCheck to check any shell code you write.
Notes
- The proper she-bang for Bash is
#!/usr/bin/env bash
.
Code
# Check if command is in PATH
checkDep() {
path=`command -v ${1}` && echo "${1} found at ${path}" || { echo "${1} not found" >&2 ; exit 1; }
}
# Get output of command. https://www.cyberciti.biz/faq/unix-linux-bsd-appleosx-bash-assign-variable-command-output/
# i.e. save output of date to var now
now=$(date)
File testing
![](https://i.imgur.com/QGkHbPm.png)
# Pipe output to file.
# i.e. pipe output of ls to output.txt
ls > output.txt
# Check if no arguments passed
if [ $# -eq 0 ] then ... fi
# Check if file does not exist
# Putting ! before makes it a not statement. Spaces before and after [] are important.
if [ ! -f ~/Desktop/file.txt ]; then
echo "File not found!"
fi
# Source vs ./
# Runs the script as an executable file, launching a new shell to run it
./script
# Reads and executes commands from filename in the current shell environment
source script
# need to wrap the cd command inside () to run it in scope of the cd
# i.e. pod install will be run inside ios dir
(cd ios && pod install)
Links
- Parameter expansion
- Defensive BASH Programming
- Safe ways to do things in Bash (HN)
- Pure Bash Bible - Collection of pure bash alternatives to external processes. (HN) (HN)
- Bash Infinity - Modern boilerplate / framework / standard library for Bash.
- Funky - Takes shell functions to the next level by making them easier to define, more flexible, and more interactive.
- create-bash-script - Bash script designed to create other bash scripts with argument parsing.
- Bash-LSP - Language server for Bash. (HN)
- Bash Reference Manual
- Bash Guide (Lobsters)
- How To Automate Basic Development Tasks with Bash
- Mini Gitbook for a presentation about BASH (Code)
- Bash $* and $@ (2017) (HN)
- Understanding Bash: Elements of Programming (2018) (HN)
- Bash tricks - Simple bash tricks which will make your life easier. (Reddit)
- Anybody can write good bash (with a little effort) (2020) (Lobsters)
- Bash Quick References
- critic.sh - Dead simple testing framework for Bash with coverage.
- Bash Hackers Wiki (HN)
- Bats - Bash Automated Testing System.
- HN: Renaming files with mv without typing the name two times
- The first two statements of your BASH script should be.. (2020) (Lobsters)
- Use Bash Strict Mode (Unless You Love Debugging)
- Bash code
- Some Relatively Obscure Bash Tips (2020) (HN)
- A Bash Cheat Sheet: Top 25 most-used commands, and how to create custom commands (2020)
- Bash Env Variable Defaults
- Using Bash traps in your scripts (2020)
- Help message for shell scripts (2020) (HN) (Lobsters)
- Supercharge Your Bash History (2020) (Lobsters)
- Import - Simple and Fast Module System for Bash and Other Unix Shells. (HN)
- Intelligent Bash (ibash)
- Functional programming in bash (Slides)
- Bash Pitfalls (Lobsters)
- argbash - Bash argument parsing code generator. (HN) (HN)
- Bash Bracket Cheat Sheet
- Bash Error Handling (HN)
- ctypes.sh - Foreign function interface for bash. (HN)
- DevHints Bash Cheat Sheet
- sbang - Run scripts with very long shebang lines. (Lobsters)
- Oh My Bash - Open source, community-driven framework for managing your bash configuration.
- An Awful Edge Case in Bash's set -e (2020)
- Variant - Turn your bash scripts into a modern, single-executable CLI app.
- Even faster bash startup (2020)
- How To Use Bash Parameter Substitution Like A Pro (2020)
- Introduction to Bash Scripting Book
- Bash Snippets - Collection of small bash scripts for heavy terminal users with no dependencies.
- Minimal safe Bash script template (2020) (Lobsters) (HN)
- Bash 5.1 (HN)
- Batsh - Language that compiles to Bash and Windows Batch. (Code)
- Bash HTTP monitoring dashboard
- Parallel bash - Parallel processing of commands in pure bash along with the support of functions.
- Learn Bash in 27 minutes
- Better BASHing Through Technology (2020)
- bashful - Use a yaml file to stitch together commands and bash snippets and run them with a bit of style.
- bash_unit - Bash unit testing.
- Elegant bash conditionals (2021) (Lobsters)
- Bash lambda - Anonymous functions and FP stuff for bash.
- How to navigate directories faster with bash (2015) (HN)
- Your
~/.bashrc
doesn't have to be a mess (2021) (Lobsters) - Bash Function Names Can Be Almost Anything (2021) (HN)
- Writing a Bash Builtin in C to Parse INI Configs (2021) (Lobsters)
- Bashly - Create beautiful bash scripts from simple YAML configuration. (HN) (Docs)
- tree-sitter-bash - Bash grammar for tree-sitter.
- Bash Boilerplate - Collection of Bash scripts for creating safe and useful command line programs.
- Bash-TPL - Smart, lightweight shell script templating engine, written in Bash.
- Bash functions are better than I thought (2021) (Reddit) (HN)
- bash-completion - Programmable completion functions for bash.
- Bash patterns I use weekly (HN)
- How to write idempotent Bash scripts (2019) (HN) (Lobsters)
- Mastering Bash and Terminal (2017) (HN)
- bish - Language that compiles to Bash. Shell scripting with a modern feel.
- Sherver - Pure Bash lightweight web server. (HN)
- Modern Bash (Zsh) Scripting (Lobsters)
- NL2Bash: A Corpus and Semantic Parser for Natural Language Interface to the Linux Operating System (2018) (Code)
- Ask HN: Let's build Check style for Bash? (2022)
- Replicating Bash Argument Splitting (2021)
- Creating a bash completion script (2018) (HN)
- 5 Modern Bash Scripting Techniques That Only A Few Programmers Know (2022)
- bpkg - Lightweight bash package manager. (Web)
- Bash One liners - Collection of handy Bash One-Liners and terminal tricks. (HN)
- CLI argument parser for Bash scripts and functions
- Type-ish - Runtime type checker for bash functions, implemented entirely in bash.
- Bash, Pipes, & Socket SDK (2022)
- Vercel Bash - Bash Runtime for Vercel serverless functions.
- Beautysh - Bash beautifier for the masses.