Interview Prep/Shell Scripting/Functions, exit codes, strict mode and traps

How do you define a function, pass arguments and return a value?

BeginnerConceptFunctions, exit codes, strict mode and traps
Answer
disk_pct() {                  # disk_pct MOUNTPOINT -> prints usage percent
  local mount=$1
  df -P -- "$mount" | awk 'NR == 2 {sub(/%/, "", $5); print $5}'
}

pct=$(disk_pct /var)          # "return" data by printing it
if (( pct > 90 )); then ...; fi

Arguments are $1, $2, ... and "$@", just like a script. return N sets the exit status (0 to 255); it is not for data. A function's status without return is the status of its last command.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Functions, exit codes, strict mode and traps