First steps in Bash scripting
You learn to turn commands you type by hand into small scripts: creating and running the file, variables, input and simple decisions.
Level 101 · 14 lessons · 12 practice labs
Lessons
The first script
- Writing commands into a file: what a script is
- The shebang and the execute permission
- Comments and readable scripts
- Short task: a script that prints the host name, the date and the disk status
Variables and input
- Variables and why you always quote them
- Capturing command output in a variable
- Reading user input with read
- $1, $2 and $#: arguments
- Short task: a script that backs up a given folder into a dated archive
Simple decisions
- if and test: does the file exist, is the number bigger
- Walking through a list with a for loop
- Reporting a script's success: exit
- Seeing what a script does with bash -x
- Capstone: write a morning check script
Practice labs
- Write a script that prints host, date and disk status: Create a script that prints the host name, today's date and how full the root partition is.
- Make an unreadable script clear with comments: Split commands crammed on one line onto separate lines, comment each step and confirm the output is unchanged.
- Turn history commands into a reusable script: Order the set-up steps in a colleague's history, drop the failed attempts and turn them into a working script.
- Fix a script's shebang and permission: Fix the first line and execute permission of a script that fails with command not found.
- Fix a script that breaks on names with spaces: Find and fix the unquoted variables in a script that fails on file names containing spaces.
- Find a script bug with bash -x: Trace a script that always takes the same branch because of a wrong comparison operator with bash -x and fix it.
- Write a script that archives a folder with a date: Write a script that backs up the folder given as an argument into a tar.gz named with today's date.
- A script that asks for input and writes a config line: Write a script that asks for a user name and department with read and appends them to a file in a set format.
- Write a script that warns about missing arguments: Complete a script expecting two arguments so it prints a usage line and exits non-zero when the count is wrong.
- Write a check that creates a missing file: Write an if check that reports when the config file exists and copies it from the template when it does not.
- Check every user on a list: Write a for loop that checks whether each user on a list has a home directory and reports the missing ones.
- Write a morning check script: Write a script that checks disk fullness, stopped services and recent error lines and exits non-zero if anything is wrong.
About this course
You learn to turn commands you type by hand into small scripts: creating and running the file, variables, input and simple decisions.
What you will learn
- The first script
- Variables and input
- Simple decisions
Prerequisites
No prior knowledge is needed; the course starts from zero.
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 beginner level; every term is explained where it first appears.
- 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.