0
0
Bash Scriptingscripting~15 mins

Debugging with PS4 in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Debugging Bash Scripts Using PS4
📖 Scenario: You are writing a bash script to calculate the total price of items bought. Sometimes the script does not work as expected. You want to learn how to debug it easily.
🎯 Goal: Learn how to use the PS4 variable and set -x to debug bash scripts by showing detailed command execution steps.
📋 What You'll Learn
Create a bash script with variables for item prices
Add a PS4 variable to customize debug output
Enable debugging with set -x
Calculate the total price using arithmetic
Print the total price
💡 Why This Matters
🌍 Real World
Debugging bash scripts is essential when automating tasks on servers or your computer. Using PS4 and set -x helps find errors quickly.
💼 Career
Many IT and DevOps jobs require writing and debugging shell scripts. Knowing how to use PS4 for debugging makes troubleshooting faster and easier.
Progress0 / 4 steps
1
Create variables for item prices
Create three variables called price1, price2, and price3 with values 10, 20, and 30 respectively.
Bash Scripting
Need a hint?

Use simple assignment like price1=10 without spaces.

2
Add PS4 variable for debug prefix
Add a variable called PS4 and set it to the string '+ Line: $LINENO: ' to show the line number in debug output.
Bash Scripting
Need a hint?

Use single quotes around the string and include $LINENO to show the current line number.

3
Enable debugging and calculate total
Add set -x to enable debugging. Then create a variable total that sums price1, price2, and price3 using arithmetic expansion.
Bash Scripting
Need a hint?

Use set -x on its own line. Use $(( )) for arithmetic.

4
Print the total price
Print the value of total using echo.
Bash Scripting
Need a hint?

Use echo $total to display the total.