Skip to main content

Lab 6.3 - Functions and Advanced Scripting

Objectives​

  • Create functions
  • Use arrays
  • Handle errors

Exercise 1: Functions​

# Script with function
cat > functions.sh << 'EOF'
#!/bin/bash
greet() {
echo "Hello $1!"
}

greet "Linux"
EOF

chmod +x functions.sh
./functions.sh

Exercise 2: Arrays​

# Arrays
fruits=("apple" "banana" "orange")
echo ${fruits[0]}
echo ${fruits[@]}

Lab created on: December 2024 - Tested on Ubuntu 24.04