0
0
Bash Scriptingscripting~15 mins

set -x for trace mode in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using set -x for Trace Mode in Bash Scripts
📖 Scenario: You are writing a simple bash script to calculate the total price of items bought. You want to see each command as it runs to understand how the script works.
🎯 Goal: Learn how to use set -x to enable trace mode in a bash script and see the commands being executed.
📋 What You'll Learn
Create a variable with item prices
Enable trace mode using set -x
Calculate the total price using a loop
Print the total price with trace output visible
💡 Why This Matters
🌍 Real World
Trace mode helps developers debug bash scripts by showing each command executed. This is useful when scripts get complex or don't work as expected.
💼 Career
Knowing how to use <code>set -x</code> is important for system administrators, DevOps engineers, and anyone writing or maintaining shell scripts in professional environments.
Progress0 / 4 steps
1
Create a list of item prices
Create a bash array called prices with these exact values: 10, 20, 30.
Bash Scripting
Need a hint?

Use parentheses to create an array in bash, like array=(value1 value2).

2
Enable trace mode with set -x
Add the command set -x below the prices array to enable trace mode.
Bash Scripting
Need a hint?

Just write set -x on a new line to turn on command tracing.

3
Calculate the total price using a loop
Create a variable total set to 0. Then use a for loop with variable price to iterate over ${prices[@]} and add each price to total.
Bash Scripting
Need a hint?

Use total=0 to start. Loop with for price in "${prices[@]}". Add with total=$((total + price)).

4
Print the total price with trace output
Add a echo command to print Total price: followed by the value of total.
Bash Scripting
Need a hint?

Use echo "Total price: $total" to show the result.