📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Linux Command Line Functions in Scripts

Functions in Scripts

5 min read
Define functions with name() { ... } and call them by name. Use local VAR=value to keep variables from leaking into global scope. Functions make scripts reusable, testable, and much easier to read.

Functions in Bash

#!/bin/bash

log() {
    echo "[$(date +%H:%M:%S)] $1"
}

backup() {
    local src=$1
    local dst=$2
    cp -r "$src" "$dst" && log "Backup done"
}

log "Starting backup"
backup /var/www /backup/www
log "Done"