#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
🖥️ FREE · BASH · GETOPTS

Bash Script Boilerplate: Start Every Script the Safe Way

Name your flags and get a script skeleton that fails loudly instead of silently.

  • 🛑 set -Eeuo pipefail
  • 🚩 getopts flags and help
  • 🧹 Cleanup with trap

How to use the Bash Script Boilerplate Generator

  1. 1. Name the scriptGive it a file name and a one-line description.
  2. 2. Define flagsList letter:name pairs such as s:source; verbose and force become on/off switches.
  3. 3. Fill in main()Download, chmod +x, and write your commands where the TODO is.

What the default script contains

The default backup.sh is summarised as 3 flags in strict mode: -s for source and -d for dest, which take values, and -v for verbose, which is a switch. The script opens with set -Eeuo pipefail, so it stops on any failing command, any unset variable and any failure inside a pipe. A usage function prints help for -h, and the getopts loop reports missing values and unknown flags with a clear message.

It then checks that tar and date exist, creates a temporary folder with mktemp and removes it with a trap on exit, even if the script fails halfway. Inside main, -s and -d are checked as required before your commands run.

Why these pieces matter

  • Without -e, bash carries on after an error, and a failed cd followed by rm can delete the wrong files.
  • Without -u, a typo in a variable name silently becomes an empty string.
  • pipefail makes cmd | tee log fail when cmd fails, not just when tee does.
  • Logging to stderr keeps messages out of output you might pipe elsewhere.

We checked the generated default with bash -n for syntax errors. Run shellcheck on your finished script to catch quoting mistakes. Build individual commands with the Linux command builder, and run the script on a schedule with the cron expression generator or a systemd service.

Frequently asked questions

Does this work with sh or zsh?

It is written for bash. The shebang uses /usr/bin/env bash, so run it as ./script.sh or bash script.sh, not sh script.sh.

Why getopts and not long options like --source?

getopts is built into bash and works everywhere. Long options need extra parsing code or GNU getopt.

Is the Bash Script Boilerplate Generator free?

Yes, the Bash Script Boilerplate Generator is free. The first time you generate your first script, you are asked for your name and WhatsApp number once. After that, this and every other Uncodemy tool opens straight away without the form.

More free tools

See all free tools