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
- Save it to a file.
- Run
chmod +x filename.shwhere applicable. - Run it from the terminal.