coderef

Bash Scripting Quick Reference

Bash scripts: variables, quoting, arguments, conditions, loops, functions, string tricks and exit codes.

5 sections · 29 snippets · updated Oct 14, 2026

Click any snippet to copy it.

Script basics

Start every script right.

  • Shebang + safety flags
  • Make executable and run
  • Variables
  • Command output into a variable
  • Read input
  • Arithmetic

Arguments

Inputs to your script.

  • Positional arguments
  • Number of arguments
  • Default value
  • Require an argument
  • Script name / dir

Conditions

Branching.

  • If / else
  • String tests
  • File tests
  • Number comparisons
  • Case

Loops & functions

Repetition and reuse.

  • For over values
  • For over files
  • Range
  • Read a file line by line
  • Function
  • Arrays

Strings & exit codes

Text tricks and success/failure.

  • Length / substring
  • Replace
  • Strip a suffix / prefix
  • Upper / lower case
  • Exit status of last command
  • Exit with an error
  • Run cleanup on exit

← All quick references