Dont Rely on Positional Args
From Five Powerful Tips for Bash Scripting
Go to text →
Don’t Rely on Passing Arguments
# Instead of assuming the order of the values was provided as desired like `args` in python
# Rely on `"${@}"1 like kwargs in python
#!/bin/bash
## reads in name and age, has boolean flag "--reset" which changes age to be 1
source functions.sh
get_params "${@}"
# Each of thuese results in the below
./main.sh --name joe --reset --age 25
./main.sh --age 25 --name joe --reset
#> NAME="joe"
#> AGE="25"
#> RESET="true"
Easily Check Your Positional Arguments
name=${1:?"Error: parameter missing Name"}
age=${2:?"Error: parameter missing Age"}
./main joe
#> Error: parameter missing Age
Create a Default Value for a Variable
echo "enter your name"
read name
name=${name:-Unknown}
If a user enters blank, your $name will be set to Unknown. The dash after the colon provides a default fallback value.