Bash Introduction

Understanding the Bourne Again Shell

🐚 What is Bash?

Bash (Bourne Again Shell) is a command-line interpreter and scripting language for Unix-based systems. It processes commands, executes programs, and automates tasks through scripts, making system management efficient and powerful.


# Simple Bash command
echo "Welcome to Bash!"
                                    

Output:

Welcome to Bash!

Key Bash Concepts

💻

Shell

Interface between user and operating system

$ bash --version
📝

Commands

Instructions executed by the shell

$ ls -l
📜

Scripts

Files containing multiple commands

#!/bin/bash
echo "Script"
🔄

Automation

Execute tasks automatically

./backup.sh

🔹 Bash vs Other Shells

Bash (Bourne Again SHell) combines features from sh, csh, and ksh while adding modern enhancements. Compared to zsh, fish, or tcsh, Bash offers superior scripting compatibility, extensive documentation, and universal availability. Its balance of interactive features and scripting power makes it the default choice for system administration, DevOps, and cross-platform development. While alternative shells offer innovative features like better autocompletion or syntax highlighting, Bash's stability, standardization, and community support maintain its position as the dominant Unix shell.


# Check your current shell
echo $SHELL

# List available shells
cat /etc/shells

# Common shells:
# /bin/bash  - Bourne Again Shell (most common)
# /bin/sh    - Original Bourne Shell
# /bin/zsh   - Z Shell (macOS default)
# /bin/fish  - Friendly Interactive Shell
                            

Output:

/bin/bash

🔹 Basic Bash Syntax

Bash commands follow consistent syntax patterns that combine commands, options, and arguments. Structure includes the command name, options (usually prefixed with - or --), and arguments (files, directories, or data). Commands can be chained with ; (sequential), && (conditional), or | (piped). Understanding syntax rules including quoting, escaping, and expansion enables effective command construction. This foundation supports everything from simple file operations to complex automated workflows using combinations of multiple utilities.


# Basic command structure
command [options] [arguments]

# Examples:
ls                    # List files
ls -l                 # List with details
ls -la /home          # List all files in /home

# Multiple commands
echo "First"; echo "Second"

# Command with output
date
whoami
pwd
                            

Output:

First
Second
Wed Oct  8 14:30:22 UTC 2025
john
/home/john

🔹 Variables in Bash

Bash variables store data that can be referenced and manipulated throughout scripts and sessions. Create with name=value (no spaces), access with $name, and make available to subprocesses with export. Variables hold strings, integers, arrays, or command outputs. Special variables include $0 (script name), $1-$9 (arguments), and $? (exit status). Proper variable usage including quoting, naming conventions, and scope management creates dynamic, maintainable scripts that adapt to different environments and requirements.


# Creating variables (no spaces around =)
name="John"
age=25
greeting="Hello, World!"

# Using variables
echo $name
echo "I am $age years old"
echo ${greeting}

# Command substitution
current_date=$(date +%Y-%m-%d)
echo "Today is $current_date"
                            

Output:

John
I am 25 years old
Hello, World!
Today is 2025-10-08

🔹 Comments in Bash

Comments document script purpose, logic, and complex operations for future reference. Single-line comments start with #, while multi-line comments use : ' and ' or sequential # lines. Effective comments explain why code exists, not just what it does. They help maintainers understand design decisions, warn about caveats, and document usage instructions. Well-commented scripts are easier to debug, modify, and share with collaborators, reducing technical debt and knowledge loss over time.


# This is a single-line comment

# Display a message
echo "Hello"  # Inline comment

# Multi-line comments using : '
: '
This is a multi-line comment
You can write multiple lines here
These lines are ignored by Bash
'

echo "Comments are helpful!"
                            

Output:

Hello
Comments are helpful!

🔹 Input and Output

Bash provides multiple methods for handling input and output in scripts and interactive sessions. Use echo for output, read for input, and redirection operators (>, >>, <) for file operations. Pipes (|) connect command outputs to inputs. Understanding I/O streams (stdin, stdout, stderr) and their manipulation enables sophisticated data processing workflows. These fundamentals support everything from simple user interaction to complex log analysis and data transformation pipelines.


# Output with echo
echo "Enter your name:"

# Reading user input
read username
echo "Hello, $username!"

# Read with prompt
read -p "Enter your age: " age
echo "You are $age years old"

# Read password (hidden input)
read -sp "Enter password: " password
echo ""
echo "Password saved"
                            

Output:

Enter your name:
John
Hello, John!
Enter your age: 25
You are 25 years old

🧠 Test Your Knowledge

How do you access a variable in Bash?