Using Default Values in Bash Scripts with ${var:-default}
📖 Scenario: You are writing a simple bash script to greet users. Sometimes, the user might not provide their name. You want to make sure the script still works by using a default name when none is given.
🎯 Goal: Build a bash script that uses the ${var:-default} syntax to provide a default value for a variable if it is empty or unset.
📋 What You'll Learn
Create a variable called
username with no value initially.Create a variable called
default_name with the value Guest.Use the
${var:-default} syntax to assign a greeting name that uses username if set, or default_name if not.Print the greeting message using the chosen name.
💡 Why This Matters
🌍 Real World
Scripts often need to handle missing inputs or environment variables. Using default values keeps scripts robust and user-friendly.
💼 Career
Knowing how to use default values in bash is essential for system administrators, DevOps engineers, and anyone automating tasks on Linux or Unix systems.
Progress0 / 4 steps