Tsallaka zuwa abun ciki
Justice Tech School

Defaults and Validation

Check argument counts and give defaults with ${1:-value}.

Example
run() {
  local name=${1:-world}
  [[ $# -gt 2 ]] && { echo "too many"; return 1; }
  echo "Hello $name"
}
run
run Ada
run a b c || echo "status $?"

Result

Hello world
Hello Ada
too many
status 1

Exercise

Default for $1.

${1world}

Quiz Test

How do you test for too many args?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.