Erik Fredericks, frederer@gvsu.edu Fall 2025
Based on material provided by Erin Carrier, Austin Ferguson, and Katherine Bowers
Modify behavior of program!
E.g., cat file --> file is an argument!
cat file
file
Reference by ${n} - n is the position
${n}
n
$0
$1, $2, 1st and 2nd (respectively) arguments on command line
$1
$2
${10}
$#
$@
$*
Assignment:
age=42
filename=erik.txt
message="This is a test"
SPACING IS IMPORTANT echo "I am $age$ years old
echo "I am $age$ years old
You can use $(cmd) to grab its input
$(cmd)
Combining with variables:
num_lines=$(grep "dog" file.txt | wc - l)
Yes it matters!
Single quotes: everything treated literally
Double quotes: expand variables inside
Try: echo '$message' echo "$message"
echo '$message'
echo "$message"
Variables can be accessed like $var or ${var}
$var
${var}
age=102
echo "I am $ageyears"
echo "I am ${age}years"
What you expect won't work
Want to do 1 + 1?
$((1+1))
$((num1+num2))
Operators available:
+
-
/
*
%
++
--
**
(Can go deeper with bracket expansion or the expr keyword but that way lies pain)
expr
command to call program
echo $0 echo $1 echo $1 weeee script: var1=$1 var2=$2 echo "$var1 and $var2"
coming back to it with loops/arrays
expr 5\* 3 bc command echo "sqrt(16)" | bc