1) What is a “shell”?
- A shell is a program that lets you talk to the OS using text commands.
- Your app → shell → kernel → hardware.
- You type commands; the shell runs them and shows output.
Don’t confuse:
· Shell (bash, zsh…) = command interpreter
· Terminal emulator (GNOME Terminal, Windows Terminal) = the window you type in
· Kernel = OS core (manages CPU, memory, devices)
2) Popular shells you’ll see
- bash (Bourne Again SHell) – common on Linux; great for scripts.
- sh/dash – very small, used for fast system scripts.
- zsh – feature-rich, fancy prompts (oh-my-zsh).
- fish – friendly auto-suggestions, not POSIX-sh compatible for scripts.
- PowerShell – cross-platform, object-oriented (different style).
For BTech work, learn bash first.
3) Types of shell sessions
- Interactive: you type commands (your normal terminal).
- Non-interactive: running a script file.
- Login vs non-login: changes which config files load (next section).
4) Startup files (bash)
- Login shell reads: /etc/profile, then ~/.bash_profile (or ~/.profile).
- Interactive non-login reads: /etc/bash.bashrc, then ~/.bashrc.
- Put aliases/functions in ~/.bashrc; environment that should exist for logins in ~/.profile.
# in ~/.bashrc
alias ll='ls -alF'
export EDITOR=vim
Reload after changes: source ~/.bashrc
5) Command basics
Format
command [options] [arguments]
Examples
ls -la /etc
cp report.txt backup/report.txt
Built-in vs external
- Built-ins: part of the shell (cd, echo, export, type).
- Externals: real programs on disk (ls, grep, python).
Find which one runs:
type ls
command -v python
6) Paths & environment variables
- PATH = list of directories searched for commands (left to right).
echo $PATH
export PATH="$HOME/.local/bin:$PATH"
- Print/set variables:
echo $HOME
NAME="Rohit"
export NAME # make it available to child processes
7) Globbing & expansion (the magic)
- * any chars, ? one char, [abc] set, [a-z] range:
ls *.py
rm -i data_202?.csv
- Command substitution: $(...)
today=$(date +%F)
echo "Today is $today"
- Brace expansion:
mkdir -p logs/{2024,2025}/{jan,feb,mar}
8) Quoting rules (super important)
- Unquoted: *, $, spaces are special (expanded/word-split).
- "double quotes": keep spaces, allow $var and $(...).
- 'single quotes': literal text (no expansion).
echo "User is $USER"
echo 'User is $USER' # prints $USER literally
9) Redirection & pipes
- Redirect output/input:
ls -l > list.txt # overwrite
echo "new" >> list.txt # append
wc -l < list.txt # input from file
- Redirect errors:
make 2> errors.log
cmd > all.log 2>&1 # combine stdout+stderr
- Pipes: chain commands
dmesg | grep -i usb | less
- Copy to file and screen:
somecmd | tee output.txt
10) Job control (multitask in one terminal)
- Run in background: sleep 60 &
- List: jobs
- Stop (Ctrl+Z), resume foreground: fg, background: bg
- Kill: kill %1 (by job), kill 1234 (by PID)
11) Exit status & command chaining
- Every command returns an exit code (0 = success).
echo $?
- Run next only if previous succeeded/failed:
make && echo "OK"
make || echo "Build failed"
cmd1 ; cmd2 # always run cmd2
12) Testing & conditions (bash)
# file tests
[ -f file.txt ] && echo "regular file"
[ -d dir ] && echo "directory"
# numbers/strings
[ "$x" -gt 10 ]
[ "$s" = "hello" ]
Prefer bash’s [[ ... ]] in scripts (safer quoting):
if [[ -n "$name" && "$age" -ge 18 ]]; then
echo "Adult"
fi
13) Loops, functions, arrays (bash)
# for
for f in *.c; do
echo "Compiling $f"
done
# while
while read line; do
echo "$line"
done < file.txt
# function
greet() { echo "Hello, $1"; }
greet "Linux"
# arrays
arr=(alpha beta gamma)
echo "${arr[1]}" # beta
for x in "${arr[@]}"; do echo "$x"; done
14) Writing your first script
1. Create file hello.sh
#!/usr/bin/env bash
set -Eeuo pipefail
# -e: exit on error, -u: unset vars are errors, -o pipefail: catch pipe errors
# -E: propagate ERR traps
name=${1:-Student}
echo "Hello, $name!"
2. Make it executable & run:
chmod +x hello.sh
./hello.sh Aayush
Script pro tips
- Always begin with a shebang (#!/usr/bin/env bash).
- Use "${var}" (quote variables) to handle spaces.
- Use set -Eeuo pipefail for safer scripts.
- Use functions to structure code.
15) Permissions refresher for scripts
chmod +x script.sh # make it executable
ls -l script.sh # check rwx bits
If directory isn’t on PATH, run with ./script.sh.
16) Essential everyday commands with the shell
# navigation & files
pwd; ls -la; cd; mkdir -p proj/src; cp a.txt b.txt; mv b.txt notes.txt; rm -ri old/
# viewing/search
cat file; less file; head -n 20 file; tail -f app.log
grep -R "ERROR" logs/
# find + xargs
find . -name "*.tmp" -print0 | xargs -0 rm -f
# system info
whoami; id; uname -a; df -h; du -sh *; free -h; top
# networking
ip a; ping -c 3 8.8.8.8; curl -I https://example.com
# package (Debian/Ubuntu)
sudo apt update && sudo apt install tree
17) Shortcuts that save time
- Tab: auto-complete file/command.
- Ctrl+A / Ctrl+E: start/end of line.
- Ctrl+U / Ctrl+K: cut to start/end; Ctrl+Y paste.
- Ctrl+R: reverse search in history.
- !!: run last command; !git run last command starting with “git”.
18) Mini-labs (hands-on, ~45–60 min)
Lab A: Shell warm-up
mkdir -p ~/lab/shell101 && cd ~/lab/shell101
echo -e "alpha\nbeta\ngamma" > words.txt
grep -n "a" words.txt | tee hits.txt
wc -l hits.txt > report.txt
Lab B: Safe cleanup script
#!/usr/bin/env bash
set -Eeuo pipefail
DIR=${1:-.}
find "$DIR" -type f -name "*.tmp" -print
read -p "Delete these files? [y/N]: " ans
if [[ $ans == y* ]]; then
find "$DIR" -type f -name "*.tmp" -delete
echo "Cleaned."
else
echo "Aborted."
fi
Lab C: Logs pipeline
dmesg | grep -i -E "usb|eth|nvme" | tail -n 50 > last_hw_events.txt
19) What to know for exams/interviews
- Definition & role of shell; difference from terminal & kernel.
- Command structure, PATH, environment variables.
- Quoting rules; piping & redirection; job control.
- Exit codes, && / ||, basic tests & conditionals.
- Basics of bash scripting: shebang, chmod +x, "$@", set -euo pipefail.
If you’d like, I can turn this into a 2-page cheat sheet or a step-by-step lab worksheet (WSL/Linux) for your first-year class.