0
0
Linux CLIscripting~15 mins

Shell options (set -e, set -x) in Linux CLI - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Shell Options: set -e and set -x
📖 Scenario: You are writing a small shell script to automate a simple task on your computer. You want to make sure the script stops if any command fails and also want to see each command printed as it runs to understand what is happening.
🎯 Goal: Learn how to use the shell options set -e to stop the script on errors and set -x to print commands as they run.
📋 What You'll Learn
Create a shell script with a few commands
Add set -e to stop on errors
Add set -x to print commands as they run
Observe the script behavior with these options
💡 Why This Matters
🌍 Real World
Shell scripts often automate tasks on computers. Using <code>set -e</code> helps avoid continuing after errors, and <code>set -x</code> helps see what the script is doing step-by-step.
💼 Career
Many IT, DevOps, and developer jobs require writing and debugging shell scripts. Knowing these options helps write safer and easier-to-debug scripts.
Progress0 / 4 steps
1
Create a simple shell script with commands
Create a shell script called script.sh with these exact commands in order: echo "Start script", ls /tmp, and echo "End script".
Linux CLI
Need a hint?

Write each command on its own line exactly as shown.

2
Add set -e to stop script on errors
Add the line set -e at the top of script.sh to make the script stop if any command fails.
Linux CLI
Need a hint?

Place set -e as the very first line in the script.

3
Add set -x to print commands as they run
Add the line set -x immediately after set -e in script.sh to print each command before it runs.
Linux CLI
Need a hint?

Put set -x right after set -e at the top.

4
Run the script and observe the output
Run bash script.sh in the terminal and observe that each command is printed before it runs and the script stops if a command fails.
Linux CLI
Need a hint?

Use bash script.sh to run the script and watch the commands printed with a plus sign (+) before each.