Skip to content

Instantly share code, notes, and snippets.

@chu-yik
Last active November 7, 2021 03:34
Show Gist options
  • Save chu-yik/e5a6fe385d0539a4f03b7781e044651b to your computer and use it in GitHub Desktop.
Save chu-yik/e5a6fe385d0539a4f03b7781e044651b to your computer and use it in GitHub Desktop.
Shell script cheatsheet

-z

-z to test if string is null (zero length)

if [ -z "$AWS_ACCOUNT_ID" ]; then
    echo "AWS_ACCOUNT_ID environment variable is not set."
    exit 1
fi

set -e

  • set -e to stop on first error (command return with non-zero retval).
  • set +e to turn this off.
  • Reference

Check command is present

check_command() {
  if ! [ -x "$(command -v "$1")" ]; then
    echo "$1 is required to run this script"
    exit
  fi
}

# usage
check_command aws
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment