A collection of practical Bash scripting assignments designed to develop problem-solving, automation, validation, and error-handling skills.
Each homework task provides:
- A student challenge sheet
- Clearly defined script requirements
- Testing instructions
- A separate solution guide
- A downloadable task package
| Homework | Topics | Resources |
|---|---|---|
| Task 1 | for loops, while loops, command-line arguments, package checks, root validation, and error handling |
Overview · Challenge · Solutions · Download ZIP |
homework/
├── README.md
├── task-1/
│ ├── README.md
│ ├── Bash-Scripting-Challenge-Tasks-1.md
│ └── Bash-Scripting-Challenge-Tasks-1-Solutions.md
└── task-1.zip
Task 1 contains five challenges requiring seven Bash scripts:
| Task | Script | Main Skill |
|---|---|---|
| 1 | for_loop.sh |
Loop through a list of fruits |
| 1 | count.sh |
Print numbers with a for loop |
| 2 | countdown.sh |
Validate input and use a while loop |
| 3 | greet.sh |
Read the first positional argument |
| 3 | args_demo.sh |
Work with $0, $#, and "$@" |
| 4 | install_packages.sh |
Check and install Linux packages safely |
| 5 | safe_script.sh |
Apply exit statuses and error handling |
After completing the homework, students should be able to:
- Create and execute Bash scripts.
- Use
forandwhileloops. - Read and validate user input.
- Process command-line arguments.
- Quote variables and arguments safely.
- Check command success and failure.
- Send error messages to standard error.
- Return meaningful exit statuses.
- Verify root privileges before administrative operations.
- Apply basic defensive scripting practices.
- Open the challenge sheet.
- Read one task carefully.
- Write the required script independently.
- Check its syntax with
bash -n. - Test both successful and failure cases.
- Check the exit status with
echo $?. - Review the solution only after completing your attempt.
- Improve your script based on what you learned.
Open the homework directory:
cd homeworkOpen Task 1:
cd task-1Read the assignment:
less Bash-Scripting-Challenge-Tasks-1.mdUse a Markdown preview in GitHub or your code editor for the best reading experience.
Make a script executable:
chmod +x script.shRun it:
./script.shYou can also run it directly with Bash:
bash script.shCheck a script without executing it:
bash -n script.shCheck all Bash scripts in the current directory:
for script in ./*.sh
do
bash -n "$script" || exit 1
done
echo "All scripts passed syntax checking."Run a script in trace mode:
bash -x script.shbash -nchecks syntax.bash -xdisplays commands as Bash executes them.
Check the status returned by the most recent command:
echo $?| Status | Meaning |
|---|---|
0 |
Success |
| Non-zero | Failure or another special result |
- Attempt every task before reading its solution.
- Use meaningful variable names.
- Quote variable expansions when appropriate.
- Add comments that explain the purpose of important code.
- Test valid, invalid, empty, and missing input.
- Send error messages to
stderrusing>&2. - Never assume that a command succeeded.
- Do not run administrative scripts without reviewing them.
The package-installation exercise can modify system software. Run it only on a suitable Linux practice environment after reviewing the script:
sudo ./install_packages.shDo not test package installation on an important production system.
- All required scripts have been created.
- Every script begins with a Bash shebang.
- Scripts pass
bash -n. - Successful and failure cases have been tested.
- Variables and arguments are quoted correctly.
- Invalid input produces a clear error.
- Failure paths return non-zero exit statuses.
- Administrative scripts verify root access.
- Scripts include useful comments.
- Solutions were reviewed only after attempting the challenges.
Created for Bash scripting practice by Muhammad Khalid Khan.