Introduction to Bash
Bash is a command interpreter: a program that reads the commands you type (or the commands stored in a file), works out what they mean, and asks the operating system to carry them out. It is the default interactive shell on most Linux distributions, it is available on macOS and on Windows through WSL, and it is the language behind a huge number of automation scripts, deployment tools, and build pipelines. Learning Bash gives you direct control over a computer using text, which is faster, more repeatable, and easier to automate than clicking through a graphical interface.
Understanding the Concept
A computer's operating system is built from two layers that matter here. The kernel is the core of the system. It manages memory, schedules programs, and talks to the hardware. Programs do not usually talk to the kernel directly in a way a human can type. Instead, you need an intermediary that accepts human-readable text and turns it into requests the system understands. That intermediary is a shell, and Bash is one of the most widely used shells.
When you type a command such as ls, Bash does the following:
- It reads the line you typed.
- It breaks the line into words and applies its own rules (expansions, quoting, redirections).
- It decides whether the first word is something Bash handles itself or a program stored on disk.
- It starts the work and waits for it to finish, then shows you the result.
Because Bash is also a programming language, the same mechanism works for files. Instead of typing one command at a time, you can save many commands in a file, called a script, and run the whole file. Everything you can type at the prompt can go into a script, and everything in a script can be tried at the prompt.
What Bash Lets You Do
Bash is useful for three broad categories of work:
- Interactive work. Navigating folders, inspecting files, searching text, and running programs from a terminal.
- Automation. Combining commands into scripts that rename hundreds of files, back up a database every night, or check whether a website is online.
- Gluing programs together. Bash can send the output of one program into another program with a pipe, so small tools each doing one job can be combined into larger solutions.
Here is a small example of the third idea. The command below prints a sentence, converts it to upper case, and counts the characters:
echo "hello bash" | tr a-z A-Z | wc -c
Output:
11
The echo command writes hello bash followed by a newline. The pipe symbol | connects that output to the input of tr, which converts lower-case letters to upper case. A second pipe sends the result to wc -c, which counts bytes. The count is 11 because HELLO BASH has ten characters and the newline adds one more.
How a Bash Session Works
When you open a terminal, a Bash process starts and prints a prompt. The prompt is a short piece of text, often ending in $, which tells you that Bash is ready for input. The loop that follows is called a read-evaluate-print loop, and it works like this:
- Bash prints the prompt.
- You type a line and press Enter.
- Bash evaluates the line, which may start programs.
- Output appears in the terminal.
- Bash prints the prompt again.
You can see which shell you are running by asking for the current process name:
ps -p $$ -o comm=
Output:
bash
The special variable $$ holds the process ID of the current shell. The ps command looks up that process, and -o comm= asks it to print only the command name without a heading. If the answer is bash, you are using Bash.
Your First Look at the Language
Bash scripts are made of a few simple building blocks, which later lessons cover in depth. A short preview shows how they fit together:
greeting="Hello"
name="Asha"
echo "$greeting, $name!"
Output:
Hello, Asha!
Line by line:
greeting="Hello"creates a variable. There must be no spaces around the equal sign.name="Asha"creates a second variable.echo "$greeting, $name!"prints text. The dollar sign tells Bash to replace$greetingand$namewith the stored values before the command runs.
Bash also supports decisions and repetition. The following example loops over three names:
for city in Chennai Madurai Coimbatore; do
echo "City: $city"
done
Output:
City: Chennai
City: Madurai
City: Coimbatore
The for loop assigns each word in the list to the variable city, one at a time, and runs the commands between do and done for each value.
Why Bash Is Worth Learning
Bash appears almost everywhere that software is built or operated:
- Linux servers. Administrators use Bash to manage users, services, logs, and storage.
- DevOps and CI/CD. Build and deployment pipelines run Bash commands to compile code, run tests, and publish releases.
- Containers. Many container images include Bash, and container start-up scripts are frequently Bash scripts.
- Developer machines. Developers use Bash to run version control, start local servers, and set up projects.
The shell is also stable. Scripts written many years ago still run today, because the core language changes slowly. That makes the skills in this tutorial long-lasting.
Interactive Use and Scripts Compared
The same commands work in both modes, but the two modes serve different purposes.
| Aspect | Interactive use | Script |
|---|---|---|
| Where commands come from | Your keyboard | A file |
| Repeatability | You retype or recall from history | Run the file again at any time |
| Feedback | Immediate, one command at a time | Output for the whole run |
| Typical use | Exploring and one-off tasks | Automation and shared tools |
A script is simply the commands you would have typed, saved in a file. For example, this script prints the current date and the current folder:
#!/usr/bin/env bash
date
pwd
If you save it as info.sh and run it with bash info.sh, Bash executes the two commands in order. The first line, beginning with #!, is called a shebang. It tells the system which interpreter should run the file, and a later lesson explains it in detail.
What This Tutorial Covers
The lessons in this series move from the basics toward real automation work:
- Fundamentals: shells, terminals, installing Bash, and running commands.
- Everyday commands for files, directories, and text.
- Scripting: variables, quoting, input and output, conditions, loops, functions, and arrays.
- Reliable scripts: exit codes, error handling, and debugging.
- Advanced topics: security, testing, portability, and automation.
- Hands-on projects that combine these skills.
Each lesson builds on the earlier ones, so working through them in order gives the smoothest path from first command to working automation.