Practice Use drills for recall and labs for real operating judgment.

LAB-AUTO-02 - Bash Scripting Basics

Write, run, and improve a small Bash script that works safely in your home directory.

AUTO Automation & Scheduling

Bash Scripting Basics

Write, run, and improve a small Bash script that works safely in your home directory.

40 min INTERMEDIATE LINUX Curriculum-reviewed
Success criteria
  • Write, run, and improve a small Bash script that works safely in your home directory.
  • Repeat the workflow without copy-paste or step-by-step prompting.
Safety notes
  • Keep early scripting practice inside disposable folders in your home directory until you are confident in your commands.

Part A: The Field Guide


What This Lab Is Really About

This lab is for turning a few Bash ideas into one real workflow:

  • create a script file
  • make it executable
  • run it
  • improve it without rewriting everything

We are keeping the script small and local on purpose. Good scripting starts with dependable basics, not with risky live-system changes.


Command Reference

Bash script essentials

nano hello.sh chmod +x hello.sh ./hello.sh

Useful patterns:

A safe script skeleton

#!/bin/bash

WORKSPACE=“$HOME/script-practice” mkdir -p “$WORKSPACE” echo “hello” >> “$WORKSPACE/output.txt”


Part B: The Drill Deck

Terminal required: work inside your own home directory.


G
Guided Step by step - type exactly this and compare the result
>

Exercise G1: Create and Run a Script

  1. Go to your home directory: cd ~
  2. Create a new file: nano hello.sh
  3. Add this content:
#!/bin/bash

WORKSPACE="$HOME/script-practice"
mkdir -p "$WORKSPACE"
echo "Hello from Bash" > "$WORKSPACE/hello.txt"
echo "Saved output to $WORKSPACE/hello.txt"
  1. Save and close the file.
  2. Make it executable: chmod +x hello.sh
  3. Run it: ./hello.sh

Exercise G2: Verify the Result

  1. Read the output file:
cat "$HOME/script-practice/hello.txt"
  1. Confirm that the script created the folder and wrote the file.

Exercise G3: Add a Variable That Changes the Output

  1. Reopen the script: nano hello.sh
  2. Add a variable such as:
MESSAGE="Study session started"
  1. Replace the fixed text in your echo line with the variable.
  2. Save, run the script again, and confirm the new output.
S
Solo Task described, hints available - figure it out
>

Exercise S1: Accept a Name as Input

  1. Edit hello.sh again.
  2. Add this line near the top:
USER_NAME="$1"
  1. Update the script so it writes a line using that value.
  2. Run it like this:
./hello.sh Shekhar
  1. Confirm that the script used the name you passed in.

Exercise S2: Add a Timestamp

  1. Add another variable:
STAMP="$(date +%F_%H%M)"
  1. Use it to create a filename such as:
"$WORKSPACE/report_$STAMP.txt"
  1. Run the script twice.
  2. Confirm that you now have two separate timestamped files instead of overwriting the same one.
M
Mission Real scenario - no hints, combine multiple skills
>

Mission M1: Build a Tiny Folder Report

Create a new script named folder_report.sh that:

  1. defines a practice folder in your home directory
  2. creates the folder if needed
  3. writes the current date into a report file
  4. appends a list of files from your home directory to that report
  5. prints the path of the report when finished

Keep the entire workflow inside your own home directory. If you can explain each line of the script and run it twice without confusion, the lab is doing its job.