Skip to content

Bash Basics

Bash (Bourne Again SHell) is the most widely used shell in Linux. It allows you to interact with the operating system, automate repetitive tasks, and write powerful shell scripts. Every Linux Administrator, DevOps Engineer, Cloud Engineer, and SRE should master Bash.


Learning Path

Linux Mastery → Module 2: Linux Command Line Essentials → Lesson 2

Difficulty: Beginner

Reading Time: 25 Minutes

Course Progress

Course: Linux Mastery

Module: Linux Command Line Essentials

Lesson: 2 of 10


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand what Bash is
  • Execute commands in Bash
  • Understand Bash syntax
  • Use variables
  • Understand quoting
  • Use command substitution
  • Work with environment variables
  • Customize your Bash prompt
  • Learn Bash best practices

Prerequisites

Before starting this lesson, you should complete:

  • Module 1 – Linux Fundamentals
  • Understanding the Shell

What is Bash?

Bash stands for:

Bourne Again SHell

It is the default shell on most Linux distributions.

Bash acts as a command interpreter between the user and the Linux kernel.

User


Bash


Linux Kernel


Hardware

Whenever you type a command:

ls

Bash interprets the command and asks the Linux kernel to execute it.


Why Bash is Important

Bash is everywhere.

It is used for:

  • Linux Administration
  • DevOps Automation
  • Cloud Infrastructure
  • Kubernetes Management
  • CI/CD Pipelines
  • Docker Automation
  • Server Maintenance

If you know Bash, you can automate almost every Linux task.


Your First Bash Commands

Display current user:

whoami

Display current directory:

pwd

List files:

ls

Create directory:

mkdir rebash

Delete directory:

rmdir rebash

Display date:

date

Every command follows this pattern:

Command


Arguments


Options

Example:

ls -la /home
Part Description
ls Command
-la Options
/home Argument

Running Multiple Commands

Commands can be chained together.

Run one after another:

pwd

date

whoami

Run on one line:

pwd && date && whoami

Meaning:

  • Execute the next command only if the previous one succeeds.

Using semicolon:

pwd ; date ; whoami

Meaning:

  • Execute every command regardless of success or failure.

Bash Variables

Variables store values.

Create a variable:

NAME="REBASH"

Display:

echo $NAME

Output:

REBASH

Another example:

CITY="Hyderabad"

echo $CITY

Rules for Variable Names

Valid:

NAME

CITY

user_name

PORT

Invalid:

123NAME

my-name

first name

Built-in Environment Variables

Display current user:

echo $USER

Home directory:

echo $HOME

Current shell:

echo $SHELL

Current working directory:

echo $PWD

Hostname:

echo $HOSTNAME

PATH variable:

echo $PATH

Understanding PATH

When you type:

ls

Bash searches for the executable inside directories listed in the PATH variable.

View PATH:

echo $PATH

Example:

/usr/local/bin

/usr/bin

/bin

/usr/sbin

Search process:

User types:

ls


Bash checks PATH


Finds:

/usr/bin/ls


Executes program

Quoting in Bash

Bash supports three types of quotes.


Double Quotes

Variables are expanded.

NAME="Linux"

echo "Welcome to $NAME"

Output:

Welcome to Linux

Single Quotes

Variables are not expanded.

echo 'Welcome to $NAME'

Output:

Welcome to $NAME

No Quotes

echo Hello

Output:

Hello

Command Substitution

Store command output inside a variable.

Old style:

DATE=`date`

Recommended:

DATE=$(date)

Display:

echo $DATE

Example:

HOST=$(hostname)

echo $HOST

Command History

Display previous commands:

history

Run a previous command:

!25

Run previous command:

!!

Search history:

Ctrl + R

Auto Completion

Press:

Tab

Example:

cd Doc<TAB>

Automatically becomes:

cd Documents/

This reduces typing mistakes.


Aliases

Aliases create shortcuts.

Example:

alias ll="ls -la"

Now:

ll

works like:

ls -la

View aliases:

alias

Remove:

unalias ll

Customizing the Prompt

Current prompt:

basha@rebash:~$

Temporarily change:

PS1="REBASH> "

Output:

REBASH>

Useful for demonstrations and scripting.


Exit Status

Every Linux command returns an exit code.

Display it:

echo $?

Common values:

Exit Code Meaning
0 Success
Non-zero Failure

Example:

mkdir demo

echo $?

Output:

0

Production Perspective

Bash powers many production workflows.

Examples:

kubectl get pods

docker ps

git pull

terraform apply

ansible-playbook site.yml

systemctl restart nginx

Almost every DevOps automation begins with Bash.


Hands-on Lab

Task 1

Display:

echo $USER

echo $HOME

echo $PWD

echo $SHELL

Task 2

Create variables.

NAME="REBASH"

CITY="Hyderabad"

echo $NAME

echo $CITY

Task 3

Display PATH.

echo $PATH

Task 4

Practice command substitution.

TODAY=$(date)

echo $TODAY

Task 5

Create alias.

alias ll="ls -la"

ll

Task 6

Check exit code.

mkdir test

echo $?

rm -r test

echo $?

Mini Challenge

Without referring to the lesson:

Create variables:

  • Your Name
  • Company
  • City

Print:

Welcome <Name>

You work at <Company>

Current Directory

Today's Date

Hostname

Use:

  • Variables
  • Command substitution
  • echo

Best Practices

  • Use meaningful variable names.
  • Prefer $(command) over backticks.
  • Use aliases for frequently used commands.
  • Understand exit codes before scripting.
  • Learn keyboard shortcuts to improve productivity.

Common Mistakes

❌ Forgetting $ while reading variables.

✅ Use:

echo NAME

Outputs:

NAME

Correct:

echo $NAME

❌ Using spaces around =.

✅ Incorrect:

NAME = Linux

Correct:

NAME="Linux"

❌ Using single quotes when variable expansion is required.

✅ Incorrect:

echo '$USER'

Correct:

echo "$USER"

Interview Questions

Beginner

  1. What is Bash?
  2. What does Bash stand for?
  3. How do you create a variable?
  4. What is PATH?
  5. What is command substitution?

Intermediate

  1. Explain Bash command execution.
  2. Difference between single and double quotes.
  3. What is an alias?
  4. Explain exit codes.
  5. Why is PATH important?

Architect Level

  1. Why is Bash still relevant in Cloud Computing?
  2. How does Bash improve DevOps automation?
  3. Why should engineers understand shell environment variables?

Summary

In this lesson, you learned:

  • What Bash is
  • Running commands
  • Variables
  • Environment variables
  • PATH
  • Quoting
  • Command substitution
  • Aliases
  • Exit codes
  • Bash productivity tips

These concepts form the foundation for Bash scripting, Linux administration, and DevOps automation.


Key Takeaways

  • Bash is the default shell on most Linux systems.
  • Variables store values and make scripts reusable.
  • Environment variables configure your shell environment.
  • PATH determines where Bash looks for executables.
  • Command substitution allows commands to be embedded inside other commands.
  • Understanding exit codes is essential for writing reliable automation.

What's Next?

Navigating the Filesystem

In the next lesson, you'll learn:

  • Absolute vs Relative Paths
  • cd
  • pwd
  • ls
  • Directory navigation
  • Hidden files
  • Tab completion
  • Efficient filesystem navigation techniques