Standard shebang header

Update Bash profile

echo 'export PATH="/some/path:$PATH"' >> ~/.bash_profile

Run in same directory

#!/usr/bin/env bash
#
cd $(dirname "$0")

But this won’t work if there are spaces in the directory spec.

Extract filename etc

filename=$(basename "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"

Change file extension


Alias’s

# Q: Why isn't alias working ?
# A: Because  don't work in non interactive mode.
#
#
shopt -s expand_aliases
alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
alias chrome-canary="/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary"
alias chromium="/Applications/Chromium.app/Contents/MacOS/Chromium"

Adding help

#!/bin/bash
#
# captureScreenshots
#
usage() {
    cat <<EOM
    Usage:
    $(basename $0)  URL filename-without-extension
EOM
    exit 0
}


if [[ ( $@ == "--help") ||  $@ == "-h" ||  $@ == "-?" ]];
then
  usage;
  exit 0
fi

Defaulting parameters

url=${1-https://ontaap.herokuapp.com/demo/index.html}
filename=${2-screenshot}

Fetch and run a gist

#!/usr/bin/env bash

function curlsource() {
        f=$(mktemp -t curlsource)
        curl -o "$f" -s -L "$1"
        source "$f"
        rm -f "$f"
    }
    
curlsource https://raw.githubusercontent.com/DEADBEEF/raw

Yes / No input

#!/usr/bin/env bash

read -p "yes or no: " RESPONSE

case "$RESPONSE" in
  yes|y|Yes|Y)
    echo "blah is yes"
    ;;
  no|n|No|N)
    echo "blah is no"
    ;;
  *)
    echo "You need to say yes or no, start over!"
    exit 1
    ;;
esac

Options

Gleaned from here

set -o   # Show current settings
set -o xtrace # Debug
set -o errexit # Exit on error
set -o autocd # Auto cd (Works with [TAB] completion)
set -o errunset # Variabe used before set

File name extraction

filename=$(basename "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"
  • See [[zsh]].