Skip to content

Bash Functions

Functions name a block of commands so you can reuse it and keep scripts tidy.

Example

#!/bin/bash
greet() {
  local name="$1"
  [[ -z "$name" ]] && return 1
  echo "Hello, $name!"
}
greet "$1" || { echo "Usage: $0 <name>"; exit 1; }

How to run it

  1. Save it to a file.
  2. Run chmod +x filename.sh where applicable.
  3. Run it from the terminal.

← Back homeRun in Sandbox →