Bash (Linux Shell) Fundamentals

Date:

This note is a practical + conceptual guide to working in a Linux shell using Bash. It covers the mental model of a shell, the Linux filesystem layout, the command patterns you will use daily, and the minimum scripting + editor skills needed to be productive.


WSL 2 (Linux on Windows)

WSL 2 (Windows Subsystem for Linux 2) runs a real Linux kernel inside Windows using lightweight virtualization. It is useful when you want a Linux environment (Bash, apt, SSH, build tools) without dual boot or a full virtual machine.

Install (PowerShell as Administrator):

wsl --install

Verify distribution and version:

wsl --list --verbose

Set WSL 2 as default (if needed):

wsl --set-default-version 2

Launch Linux:

wsl

The Unix Shell Model

A shell is a program that reads commands, parses them, and launches other programs. Bash is both:

  • an interactive command interpreter (type commands, get output), and
  • a scripting language (save commands in a .sh file and automate work).

A key idea: the shell orchestrates programs. Most commands are small tools that do one job well, and you combine them using pipes and redirection.

Command form

command [options] [arguments]

Example:

ls -l
  • options modify behavior (e.g., -l, -r, --all)
  • arguments are targets (files, directories, values)

Linux Filesystem Structure

Linux uses a single directory tree rooted at / (pronounced “root”). There are no drive letters. Instead, storage devices are mounted somewhere in the tree.

/ (root of the filesystem)
Top-level directory. Everything is inside it.

/home (user home directories)
Where normal user files live. Example: /home/username. Most of your personal projects and documents belong here.

/root (root user’s home directory)
Home directory for the administrator user root. Regular users do not work here unless you are doing system admin tasks.

/bin (essential user commands)
Basic commands needed for system operation and recovery (e.g., ls, cp, mv). On many modern systems /bin is linked into /usr/bin, but the concept remains useful.

/sbin (system administration commands)
Admin-oriented binaries (often require elevated privileges). Similar note: may be linked into /usr/sbin on modern distros.

/usr (userland applications and libraries)
Large set of installed programs and shared resources. Common subpaths:

  • /usr/bin executables,
  • /usr/lib libraries,
  • /usr/local locally installed software (manual installs, custom builds).

/etc (system configuration)
System-wide configuration files (text files). Many services are configured here.

/var (variable data)
Data that changes often: logs, caches, queues, spool files. Examples:

  • /var/log log files
  • /var/cache caches used by package managers and services

/tmp (temporary files)
Short-lived temporary files. Often cleaned automatically on reboot or by system services.

/dev (devices as files)
Device nodes representing disks, terminals, and peripherals.

/proc (process + kernel info, virtual filesystem)
A live view into running processes and kernel state. Files are generated dynamically.

/opt (optional/third-party software)
Common place for “bundled” or vendor software installs.

This layout matters because it tells you where to look: logs (/var/log), configs (/etc), executables (/usr/bin), user projects (/home).


Daily Terminal Workflow

Most work starts with knowing where you are and what is around you.

Print current directory:

pwd

List contents:

ls
ls -l
ls -a
ls -lh

Change directory:

cd /
cd /home
cd ..
cd -
  • cd - switches back to the previous directory (fast toggling)

Create and remove directories:

mkdir project
rm -r project

Create, copy, move, delete files:

touch file.txt
cp file.txt backup.txt
mv file.txt renamed.txt
rm file.txt

Viewing and inspecting files

You will constantly inspect configs, logs, and outputs.

cat file.txt
less file.txt
head -n 20 file.txt
tail -n 50 file.txt
tail -f /var/log/some.log
  • less is the default safe viewer for large files
  • tail -f follows a file as it grows (log monitoring)

Searching and locating things

Searching is a core terminal skill: you search text, and you search for files.

Search inside files:

grep "error" file.txt
grep -n "error" file.txt
grep -r "keyword" .
  • -n shows line numbers
  • -r searches recursively through a directory tree

Find files by name/pattern:

find . -name "*.txt"
find /var/log -type f -name "*.log"

Locate the executable used when you type a command:

which python
which git

This helps debug “why is the wrong version running?” issues.


System Understanding and Monitoring

These commands help you reason about the machine: OS/kernel, disk, CPU, and processes.

System information:

uname
uname -a

Disk usage overview:

df -h

Process listing (snapshot):

ps aux

Real-time process monitor:

top

A more readable interactive monitor (often preferred):

sudo apt install htop
htop

Stop a process (when something hangs or misbehaves):

kill PID
kill -9 PID
  • kill PID sends a default termination signal (graceful)
  • kill -9 force-kills (use when needed; it skips cleanup)

Networking and Remote Access

Terminal work often involves servers, cloud VMs, and APIs.

Connectivity test:

ping example.com

HTTP requests and downloads:

curl https://example.com
wget https://example.com/file.zip

Remote login (Secure Shell):

ssh user@server_ip

SSH is the standard way to administer Linux servers. It also underpins Git over SSH and many deployment workflows.


Package Management (APT)

On Debian/Ubuntu, APT manages software installation and updates. The key idea: packages come from configured repositories, and APT resolves dependencies automatically.

Update local package index (do this first):

sudo apt update

Upgrade installed packages:

sudo apt upgrade

Install a package:

sudo apt install package_name

Search packages:

apt search keyword

A good habit is: apt update → then apt install / apt upgrade.


Pipes and Redirection

The most powerful shell concept is composition: you chain small tools.

Write output to a file:

ls > output.txt

Append output:

ls >> output.txt

Pipe output into another command (filtering):

ls -l | grep ".txt"

Read this as: “List files in long format, then keep only lines containing .txt.”


Environment Variables

Environment variables are key-value pairs inherited by processes. They configure tool behavior (paths, credentials, modes).

View environment variables:

printenv

Set a variable for the current shell session:

export VARIABLE=value
echo $VARIABLE

Tip: variables set like this usually disappear when you close the terminal unless you persist them in shell startup files (e.g., ~/.bashrc).


Vim Essentials

Vim is a modal text editor commonly available on Linux servers. It is fast once you know the modes.

Open a file

vim file.txt

Modes

  • Normal mode: navigation + commands (default on open)
  • Insert mode: type text
  • Visual mode: select text

Must-know keys

  • i → enter Insert mode
  • Esc → return to Normal mode
  • :w → save
  • :q → quit
  • :wq → save and quit
  • :q! → quit without saving

Navigation

  • h j k l → left, down, up, right
  • gg → top of file
  • G → bottom of file
  • /pattern → search forward for pattern
  • n / N → next/previous match

Editing basics

  • dd → delete current line
  • yy → copy (yank) current line
  • p → paste after cursor
  • u → undo
  • Ctrl+r → redo

Vim is worth knowing because many servers do not ship with GUI editors, and quick edits to configs/logs are common.


Bash Scripting Basics

A Bash script is a file of commands executed in order. This is how you automate repeatable tasks.

Create script.sh:

#!/bin/bash
echo "Hello World"

Make it executable and run:

chmod +x script.sh
./script.sh

A minimal best practice is to start with #!/bin/bash (shebang), which tells the OS how to execute the file.


Paths and Patterns

Absolute path starts from /:

/home/user/documents

Relative path starts from where you are:

../documents

Wildcards

  • * → matches many characters
  • ? → matches one character

Example:

rm *.txt

Closing Notes

A professional Linux workflow is built on a few principles:

  • know the filesystem layout (/etc, /var/log, /home),
  • learn to search efficiently (grep, find),
  • observe systems with the right tools (df, top, htop),
  • automate routine work with scripts, and
  • use pipes/redirection to compose simple tools into powerful pipelines.