Skip to content

Instantly share code, notes, and snippets.

@apinstein
Last active December 14, 2015 00:08
Show Gist options
  • Save apinstein/4996320 to your computer and use it in GitHub Desktop.
Save apinstein/4996320 to your computer and use it in GitHub Desktop.
Shell Scripting best practices

Ensure a consistent execution environment

  1. Always start the script with a shebang line

    #!/bin/bash
    
  2. Always run the script directly

    chmod +x path/to/script.sh
    path/to/script.sh
    

Error out early

# causes script to exit with error code of any failed command, even in a pipeline
set -e

doesnotexist | grep foo
=> [1] line 5: doesnotexist: command not found

Debugging tips

# echo every line before execution
set -x

CRON

cron jobs should ...

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