> See 'return codes' from Commands Inside a Chain of Pipes (pipeline)

It can be hard to tell if everything went okay inside a chain of pipes since $? will only report the return code from the final command in the chain. Using a different variable ($PIPESTATUS) the return codes of all the commands chained together can be found. #!/bin/bash # as long…

> Track BASH script command failures or abort after N failures

Using the trap built-in signals received by a scripts (e.g. SIGINT). Using this functionality a script can also take actions based on errors. This can allow some more nuanced handling as opposed to set -e. #!/bin/bash ErrorThreshold=5 Cumulative_RC=0 ErrorCount=0 TrackErrorTrap() { ((Cumulative_RC += $?)) ((ErrorCount++)) if…

> Best Way to Copy Dotfiles

I've seen a lot of convoluted solutions floating around regarding how to copy files starting with .. The problem everyone is trying to avoid is that when you run something like: cp -r A/.* B/ The above command starts copying everything the entire "A" directory (.) and then from the parent directory…

> A Couple Ways to Return a Random Number on the Command Line

Lets explore a couple ways to get random numbers on the command line. Anyone who attempts to generate random numbers by deterministic means is, of course, living in a state of sin. John von Neumann $RANDOM Bash Internal $RANDOM is an internal Bash function (not a constant) that returns a…

> Re-call Command Line Arguments in BASH

BASH's history expansion character, ! has many features, including !: for choosing a specific argument (or range of arguments) from the history. The gist is any number after !: is the number of the argument you want, with !:1 being the first argument and !:0 being the command. echo one two three one…

> Advanced BASH Variable Usage

Basic Variable Usage $VARIABLE ${VARIABLE} BASH variables are case sensitive, e.g. $VARIABLE is not the same as $variable. The curly bracket syntax allows the variable name to be delimited from trailing characters that otherwise might be interpreted as part of the variable name: VARIABLE=end echo "The opposite of…

> How a BASH Script Can Find Its Own Location

Most foolproof method I'm aware of is the following: SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd && echo x)" SCRIPTDIR="${SCRIPTDIR%x}" or if you're not worried about newlines at the end of your directory names: SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" How does…

> Exploring the BASH_SOURCE Array

$BASH_SOURCE is the call stack of scripts with the top of the stack being the currently executing sourced script, sourcing another script from the current one will push it on top of the stack. To get an idea of how this works you can set up a directory tree…

> Print out Line Numbers in a BASH Script

The environment variable $LINENO contains the current line number of the running BASH shell. On the command line the value will be the number of command line prompts you've used in that shell, but in a script it's the currently executing line -- and it really is line number, if…