Wiki

Table Of Contents

  1. Bash
    1. Script Directory
    2. Check
    3. Bold Echo
    4. Die
    5. getopts Shift

Bash

Script Directory

Determine the directory in which a bash script is located:

prog_dir=$(dirname $(realpath "$0"))
      

Check

Check the previous command succeeded and print an error message if it failed:

check() {
    if [[ "$?" != "0" ]]; then
	   die "$@"
    fi
}
      

Bold Echo

Print bold text:

bold() {
    echo "$(tput bold)$@$(tput sgr0)"
}
      

Die

Print error to stderr and exit:

die() {
    echo "Error: $@" >&2
    exit 1
}
      

getopts Shift

Shift after getopts:

shift $((OPTIND-1))