Erik Fredericks, frederer@gvsu.edu Fall 2025
Based on material provided by Erin Carrier, Austin Ferguson, and Katherine Bowers
for var in list; do # loopy things done
Samples:
for var in 1 2 3 4 5 for var in "alice" "bob" "erik" for val in {0..10..2} for var in $@
Remember that $(cmd) will run cmd
$(cmd)
cmd
var=$(ls) for idx in $(seq 0 10); do echo $idx done
while CONDITION; do # while things done
CONDITION? Same as if!
CONDITION
if
getopts
Flags/switches
-f -r -t
-d /path/to/dir
"this is a string being passed in as a single argument"
sh script.sh –f –r –t –d /path/to/dir "this is that string I mentioned"
OPTSTRING
VARNAME
getopts OPTSTRING VARNAME getopts abc:d Allows:
getopts OPTSTRING VARNAME
getopts abc:d
-a
-b
-c <SOME DATA>
-d
getopts :abc:d
Parses arguments in order
while getopts :ci opt; do case $opt in c) echo “-c found” ;; i) echo “-i found” ;; \?) echo “invalid option -$OPTARG” >&2 ;; esac done
Add colon (:) after flag to indicate an expected value
:
t
while getopts :csi: opt; do case $opt in c) echo “-c found” ;; s) echo “-s found” ;; i) echo “-i found with argument $OPTARG” ;; \?) echo “invalid option -$OPTARG” >&2 ;; esac done
User input : read from command line
echo –n "Enter your name: " # -n skips the newline read your_name echo "Hello $your_name"
read by default dumps everything into single variable ($your_name in this case)
read
$your_name
read –p "Enter your name: " first_name last_name echo “Your name is $first_name $last_name
May wish to provide a way to give a default response if the user isn’t watching
read –t 5 –p "Should we auto-continue [Y/n]? " answer case $answer in N | n) echo "Exiting..." exit ;; Y | y | *) echo echo "Continuing on..." ;; esac
arr=() # Empty array declare -a arr_2 # Also empty arr_3=(10 8 6) # Create with values
arr[0]=test # Set value arr_2+=(87) # Append value arr_3+=(4 2) # Append values
Accessing:
${arr[0]} # Access nth element
Special accessing:
${arr[@]} # All elements ${arr[*]} # All elements as string ${#arr[@]} # Count of elements ${!arr[@]} # Indices of elements ${arr[@]:start:num} # Slice of array
for x in ${arr[@]}; do echo "$x" done
declare -A dnd_stats dnd_stats=( [str]=16 [dex]=10 [con]=18 [int]=11 [wis]=10 [cha]=14 ) echo "wisdom = ${dnd_stats[wis]}" dnd_stats[wis]=$((dnd_stats[wis] + 2)) echo "wisdom = ${dnd_stats[wis]}"
show junk for suppress…just plop hand on keyboard args
Show that –c –i and –ci both works but not csi Show sh 1> OK.txt 2> ERR.txt basic-getopts.sh silent → handle errors by yourself
Show without flag Can be accessed as $1