Skip to main content
Imagine running the same sequence of commands dozens of times to create users, check services, or process files. Imagine scrolling through hundreds of log lines trying to find a single error. That wastes time and is error-prone. This lesson shows how to automate repetitive command-line work using Bash scripting and how to extract precise information with regular expressions. We’ll turn commands you already know into small automation tools, then add conditionals and loops so scripts can make decisions and repeat actions. Finally, you’ll learn how regular expressions let you pull exactly the information you need from files and logs. By the end, you won’t just be using the command line — you’ll be automating it. Why automate the command line?
  • Save time by bundling repeated sequences of commands into reusable scripts.
  • Reduce human error when performing administrative tasks.
  • Make outputs predictable and easier to parse with tools and regular expressions.
We begin with the idea of combining commands into a script: instead of typing a sequence of commands every time, place them into a file and treat that file like a tool.
A dark-themed diagram titled "Scripting" that shows five terminal command icons labeled cmd1 through cmd5 all feeding into a single script file icon. It illustrates combining separate commands into a reusable workflow.
What is a script? A script is simply a sequence of commands saved in a file. For example:
You are not learning brand-new commands — you are learning how to organize and automate them. To behave like a normal command, a script needs execute permissions. Without execute permission it remains a plain text file. Once executable, the system treats it like any other program and you can run it by calling its path. Make your script executable
  • Create the file, then grant execution rights:
    • chmod +x ./script.sh
  • Run it with its path:
    • ./script.sh or /path/to/script.sh
Core scripting concepts Three core elements make scripts functional and flexible:
  • Exit codes — Every command returns a numeric status: 0 means success; non-zero indicates failure. Scripts rely on exit codes to make flow decisions.
  • Variables — Store data inside the script (usernames, paths, service names, or command output) so you don’t hard-code values and your script becomes reusable.
  • Arguments — Allow a script to accept input from the command line (for example, $1, $@, $#) so you can change behavior without editing the script.
A dark-themed slide titled "Scripting" showing three labeled concepts — Exit Codes, Variables, and Arguments — each with a colored circular icon and brief descriptions about status codes, reusable storage, and external inputs.
Note: Use variables and arguments in code examples as inline literals (for example, use $@, $#, $0) and never put raw { or } in plain paragraph text — always wrap such snippets in code ticks.
Exit codes are essential for error handling. Use if statements or &&/|| to act based on a command’s exit status ($?).
Never run downloaded scripts from untrusted sources without inspecting them. Grant execute permission only after verification (chmod +x).
Core elements — quick reference table Practical examples Below are short, focused examples that demonstrate the core scripting concepts in a compact, reproducible way.
  1. Shebang and a simple script
Save this in a file (for example hello.sh), give it execute permission, and run it:
  1. Variables (no spaces around the =; expand with $)
  1. Arguments — positional parameters and counts
  1. Exit status of the last command
Every command in Linux returns a status code. Use $? immediately after a command to inspect its exit status. From theory to practice — building a demo script We’ll create a small script from scratch, make it executable, and run it. The sequence below shows the commands run on the shell and the contents of the script. Create and edit the script:
Inside demo.sh write:
Save and exit, make it executable, and run it:
Enhancing the script with variables Edit demo.sh to include variables and use them:
Notes on variables:
  • No spaces around the = when assigning.
  • Quote expansions when values may contain spaces ("$VAR").
  • Use braces for clarity when concatenating (${VAR}text).
Run the enhanced script:
Adding arguments to the script Add argument handling to demo.sh so it reports its name and the first two arguments:
Save, make executable (if not already), and run with two sample arguments:
Next steps (brief)
  • Add if/else and case statements to branch logic based on exit codes or argument values.
  • Use loops (for, while, until) to iterate over lists of items or files.
  • Capture and parse command output with regular expressions (grep, sed, awk) to extract precise data from logs and text files.
  • Combine set -e, set -u, and set -o pipefail in your scripts for stricter error handling.
Summary
  • Place sequences of commands into files to reuse and automate them.
  • Add #!/bin/bash at the top to specify the interpreter.
  • Make scripts executable with chmod +x.
  • Use variables for reusable values; use positional parameters ($0, $1, $@, $#) for input; inspect exit statuses with $? or check commands directly.
  • With these basics you can begin automating common administrative tasks instead of running them manually.
Further reading and references

Watch Video