Skip to content

Instantly share code, notes, and snippets.

@M1ke
Last active September 7, 2020 14:37
Show Gist options
  • Save M1ke/036e9225fd666115ede99d724b4a846e to your computer and use it in GitHub Desktop.
Save M1ke/036e9225fd666115ede99d724b4a846e to your computer and use it in GitHub Desktop.
Generally useful stuff for bash

Felt I should collate useful stuff I constantly bounce between files to look up

Expand alias (when in interactive terminal)

Ctrl + Alt + e

(Ubuntu)

Exit if variable (e.g. envvar) isn't set

[[ "${MY_VAR}" == "" ]] && { echo "Some var was not set!"; exit 1; }

Press specific key to continue

read -p "Something will happen, press y to continue: " -n 1 -r
echo    # moves to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]] ; then
  [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
fi

Does a file exist

FILE=/path/to/file
if [ ! -f $FILE ]; then
  echo "File at $FILE does not exist, exiting"
  exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment