Shell Scripting : Day #2

Table of contents
- 1. What Makes Bash Scripting Special?
- 2. Variables & Strings with a Twist
- 3. Captain's Tools: Control Flow (if, else, for, while, functions)
- 4. Passing Arguments: The Secret Language of Scripts
- 5. Exit Codes, Short-circuiting, & Command Substitution
- 6. Pattern Matching: Globbing & Curly Braces
- 7. Script vs. Function: Pick Your Weapon
- 8. Finding Files, Code, & Shell Commands—Like a Pro
- 🗂️ Finding Files
- 🔍 Finding by Content
- ⏳ Shell History Magic
- 9. Bonus Crew Tools: Fast Navigation & More
- 10. Boarding Exercises: Level Up Your Skills
- 🏴☠️ Parting Waves

Ahoy, crew—welcome to Day 2 of our shell scripting adventure! Yesterday, we set sail with the basics and now it’s time to grab the anchor and plunge a bit deeper. By the end of today, you’ll be whipping up bash scripts, using powerful search tools, and fishing files out of massive directories like a pro captain scanning the horizon!
Been a long time right? That might be a thing to fret upon but as i compensate & try to be more regular this time, here’s the link for Lecture 2: https://missing.csail.mit.edu/2020/shell-tools/
1. What Makes Bash Scripting Special?
Before we start scripting, why do we use Bash and not Python or Ruby? Well, Bash is tailor-made for shell tasks—gluing commands, handling files, and dealing with input/output in a breeze. It’s like having a toolkit designed to repair your ship, not build an entire new one.
2. Variables & Strings with a Twist
Assigning variables in Bash? Easy, but mind the syntax:
bashname="ShellSailor"
echo $name
No spaces around the =
! Otherwise, Bash gets confused and tries to run mysterious programs named name
.
Strings? Single vs. double quotes matter:
'Hello $name'
– Literal, prints$name
"Hello $name"
– Magic! Expands variable, printsHello ShellSailor
Test it out in your shell. You'll see why quotes matter!
3. Captain's Tools: Control Flow (if, else, for, while, functions)
Scripting’s real power? Making decisions and repeating tasks!
If-else Example:
bash#!/bin/bash
if [ "$name" = "ShellSailor" ]; then
echo "Welcome aboard, sailor!"
else
echo "Identify yourself!"
fi
For Loop Goodness:
bashfor i in 1 2 3; do
echo "Row $i"
done
Function Magic:
bashmake_and_cd() {
mkdir "$1" && cd "$1"
}
make_and_cd seashell
Functions let you bundle commands and pass arguments, just like a real programming language!
4. Passing Arguments: The Secret Language of Scripts
How do scripts get external info? Through special variables:
$0
: Script name$1
,$2
, ... : Arguments passed$@
: All arguments$#
: Number of arguments
bash#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Number of arguments: $#"
Run this as ./
script.sh
hello world
to see the magic.
5. Exit Codes, Short-circuiting, & Command Substitution
Every command leaves a trace as an exit code: 0
= success, anything else = trouble!
Tip: Chain commands for battle-hardened scripts:
cmd1 && cmd2
– runs cmd2 only if cmd1 succeededcmd1 || cmd2
– runs cmd2 only if cmd1 failed
To capture command output into a variable:
bashcurrent_date=$(date)
echo "Ship’s log date: $current_date"
6. Pattern Matching: Globbing & Curly Braces
Globbing lets you work with groups of files using wildcards:
*.txt
– every text file??.sh
– any 2-character.sh
file
Curly Braces: Expand repetitive commands:
bashmv file{1,2,3}.txt backup/
# moves file1.txt, file2.txt, file3.txt to backup/
Super handy for batch moves or renames!
7. Script vs. Function: Pick Your Weapon
Aspect | Function | Script |
Language bound | Must be in Bash | Any language (Python, Ruby, etc.) |
Loaded | On script start | Each execution |
Access to env | Yes (can change shell state e.g., cd ) | No (runs in sub-shell) |
Changes env vars | Yes | No |
Shebang lines (#!/usr/bin/env python3
) make sure the right interpreter runs your script!
8. Finding Files, Code, & Shell Commands—Like a Pro
🗂️ Finding Files
find . -name "*.sh"
– Find all.sh
files recursively!Modern pirates use
fd
for a smoother ride:
fd mypattern .
– much faster & prettier output
🔍 Finding by Content
grep -rn "myfunction" .
– Find every file mentioning "myfunction"Try
rg
(ripgrep) for warp speed:
rg "main"
– recursively, colorized, ignores.git
⏳ Shell History Magic
history | grep "find"
– See commands with "find"Ctrl + r
– Interactive fuzzy search in your shell historyWant more? Install
fzf
for blazingly fast and fuzzy command recall.
9. Bonus Crew Tools: Fast Navigation & More
Ship captains don't waste time typing cd
commands! Use:
fasd
/autojump
: Jump to frequently used directories with a quick shortcut (e.g.j my_project
)tree .
: Visualize your file structure (see how deep your ship’s hold goes!)nnn
/ranger
: Terminal file managers for serious looting
10. Boarding Exercises: Level Up Your Skills
Try these on your own ship:
ls
Mastery: Write a command to list all files (including hidden), colorized, by recency, with human-friendly sizes.Home Port Saver: Bash functions
port
andgotoport
that save and return you to directories.Reliability Roulette: Bash script that repeats a command until it fails, logging all output and counting attempts.
HTML Treasure: Command to recursively find all
.html
files and zip them—spaces in filenames won't sink your ship. (Hint:find
,xargs
, andzip
)File Hunter: Script to list all files by modification date—what’s been changing on your vessel?
🏴☠️ Parting Waves
Today, you’ve armed yourself with advanced Bash scripting skills, supercharged your search capabilities, and got a taste for shell power tools. Play with every code sample, tweak them, break them, and see what happens. That’s how all great shell masters are made!
Tomorrow, we voyage further—data wrangling ahoy!
Keep shellin’ & sailin’ and see you on deck for Day 3, sailor!
Bleed code!
Subscribe to my newsletter
Read articles from Epsit Bhardwaj directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Epsit Bhardwaj
Epsit Bhardwaj
Co-Organiser @GTA Ghaziabad, Organiser @CoderDojo (A Raspberry Pi Foundation Initiative), Speaker @BhopalFOSS, a Web (2&3) developer and an ML contributor.