Shell scripting (Bash)
Bash scripts with variables, conditions, loops and functions.
Level 201 · 19 lessons · 18 practice labs
Lessons
First scripts
- A first script: shebang and execute permission: You will write your first script and understand what the shebang on the first line does and why execute permission must be given explicitly.
- Fixing a script that won’t run: permissions, shebang and line endings: By checking chmod, the shebang and Windows line endings in turn, you can quickly get a failing script running again.
- Variables and user input: You will learn to define variables in a script, read input from the user, and use values correctly with quotes.
- Arguments and argument checks: You will learn to pass values to a script from the command line, and to check missing or wrong arguments up front with a clear error message.
- Scripts with options: getopts and usage messages: With getopts you can parse options such as -f and -v and print a clear usage message when the script is misused.
- Short task: write the skeleton of a backup script that checks its arguments
Decisions and loops
- Conditions and file tests: You will learn to make decisions with if, write tests such as whether a file exists, and have a script take different paths depending on the situation.
- Arithmetic and case: calculating and branching on choices: You can write a readable script that calculates with $(( )) and runs different work depending on an option using case.
- Loops: for, while, until: You will use for, while and until loops to automate repetitive work and learn which loop fits which situation.
- Exit codes and failure handling: You will learn to read command exit codes, stop a script safely when something fails, and return your own exit codes.
- Functions and local variables: You will learn to split repeated code into functions and use local variables so functions do not overwrite each other's values.
- Short task: a script that walks a list of services and reports their state
Working with data
- Parameter expansion and arrays: You will use parameter expansion to reshape variables and set defaults, and arrays to hold several values in one place.
- Associative arrays and comparing arrays: With declare -A you can build a key-value table and compare two lists to find common and different items.
- Bulk file operations: loops, mktemp and rename: With loops and rename you can rename hundreds of files in a safe order and manage temporary files cleanly with mktemp.
- Short task: a script that counts requests per IP
Robust scripts
- Signals and clean-up with trap: You will learn to catch signals with trap so a script cleans up its temporary files and exits tidily even if it is interrupted.
- Logging in scripts: You will learn to write timestamped log entries from a script and see how those records help later when investigating problems and incidents.
- Capstone: an audited user-provisioning script: For a fictional company you will write a small script that creates new user accounts, checks its input, and logs everything it does.
Practice labs
- Get a script that will not run going: Fix a script with permission, shebang and line-ending errors and confirm it gives the expected output.
- Add options to a script: Add day-count, dry-run and help options to an existing clean-up script with getopts.
- Write a script that validates user input: Write a script that asks for a port number and service name, re-asks on invalid input and writes the result as a config line.
- Write a backup script skeleton that checks arguments: Write a script that takes source and target directories and exits with a usage message on missing or invalid arguments.
- Make a script return correct exit codes: Fix a check script that always returns 0 so it stops with a meaningful exit code on a failed step.
- Stop a script running twice at once: Add a lock file to a scheduled backup script so a second copy exits at once with a clear message and a non-zero exit code.
- Walk a service list and report states: Loop over the service names in a file and report on one line each whether it is running.
- Write a small admin menu with case: Write a script that handles start, stop, status and logs with case and returns an error code for unknown choices.
- Split repeated code into functions: Rewrite a script that repeats the same block four times using functions with local variables, keeping the output identical.
- Write a script that counts requests per IP: Write a script that counts requests per IP in a given access log with an associative array and prints the top five.
- Transform file names with parameter expansion: Rename backup files by changing their extension and date prefix with parameter expansion and no external commands.
- Compare two servers' package lists with arrays: Read two package lists into arrays and report packages present on only one side under two headings.
- Find a syntax error without running the script: Check a script containing a delete command for syntax without running it, fix the unclosed block and submit the faulty line number.
- Fix a script stuck in an infinite loop: Use bash -x to find and fix the counter and condition bug in a script that never leaves its retry loop.
- Make a bulk conversion safe with temp files: Fix a conversion script that corrupts files in place so it uses mktemp and keeps the originals even if interrupted.
- Make an interrupted script clean up: Add a trap to a script that leaves lock and temp files behind when interrupted with Ctrl-C.
- Add timestamped logging to a script: Add a logging function to a silent maintenance script that writes each step to a file with a timestamp and level.
- Write an audited user-provisioning script: Write a script that creates accounts from a CSV, skips existing ones, logs every action and cleans up on error.
About this course
Bash scripts with variables, conditions, loops and functions.
What you will learn
- First scripts
- Decisions and loops
- Working with data
- Robust scripts
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.