Skip to content
Justice Tech School

Arrays and Functions

Arrays use name=(a b c) and ${name[@]} for all items. Functions are defined with name() and receive arguments as $1, $2. Return a number with return and text with echo.

Example
fruits=(apple banana cherry)
fruits+=(date)
echo "${#fruits[@]} items, first: ${fruits[0]}"
for f in "${fruits[@]}"; do echo "- $f"; done
greet() {
  echo "Hello, $1"
}
greet Ada
add() { echo $(( $1 + $2 )); }
result=$(add 4 5)
echo "sum=$result"

Result

4 items, first: apple
- apple
- banana
- cherry
- date
Hello, Ada
sum=9

Exercise

First argument inside a function.

echo 1

Quiz Test

Which variable holds the first argument?

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