Bash and Shell Scripting
From the first commands typed at a prompt to Bash scripts that run unattended, with sed, awk and tmux along the way, shell scripting as told through the free samples of Leanpub books.
Created from the free samples of 12 Leanpub books and the descriptions and tables of contents of 18 more, with every source cited.
Bash and shell scripting is the practice of driving a computer by typed commands and of saving those commands in files that run unattended. A shell is the program that reads a command and turns it into requests to the operating system; Bash, the Bourne-again shell, is the most widely used of them, and a shell script is a text file of the same commands a person would type.[2] What sets a shell language apart from most programming languages is that it was designed to glue other programs together.[2]
The field is old and in daily use. Servers and cloud infrastructure are almost always reached through a terminal session, and build pipelines, infrastructure automation and the entry points of containers are commonly shell scripts.[10] [1] Bash is not the only shell a script may meet. On Debian and the distributions derived from it the system shell at /bin/sh is Dash, a smaller shell that keeps more strictly to the POSIX standard, and Alpine, a minimal distribution made for containers, ships BusyBox ash, so a script that relies on Bash features can fail where it is deployed.[1] For interactive work there are also the Z shell, a newer shell with extra features, and fish, whose syntax is its own.[3]
This article describes the command line, the Bash language, the text-processing tools that scripts lean on, the writing of scripts that can be trusted, automation, and the terminal itself. Its sources are the free samples of 12 Leanpub books and the public descriptions and tables of contents of 18 more, published between 2014 and 2026.
Where to start
A reader who has never opened a terminal has several gentle beginnings. Really Friendly Command Line Intro (2018) is an illustrated booklet for people learning to code on a Mac, which treats the command line as the first step in learning to program and deliberately leaves out technical detail.[6] POSIX Shell Scripting from Scratch (2026) requires no command-line or programming experience and estimates 20 to 30 hours for a complete beginner.[3] Beginner's Guide to GNU/Linux Scripting (2020) is arranged as lessons of an hour or less, and The Hacker Ways (2014) teaches the terminal by starting a small web site in it.[13] [4] Linux and Command Line: Your Production Survival Kit (2026) starts from scratch for testers and junior developers.[7]
Someone who already runs commands and wants to understand Bash has Learn Bash the Hard Way (2019), which assumes basic shell use and requires every exercise to be typed out.[2] The Ultimate Linux Shell Scripting Guide (2025) is written for programmers who know another language and are comfortable at the command line but have not explored Bash.[15] Introduction to Bash Scripting (2023) is an open-source guide to the basics.[14]
A reader who already writes scripts can go to Reliable Bash Scripting (2026), aimed at people with scripts in production that they are nervous about touching, and to Pure Bash Bible (2018), a reference of functions that use only what Bash has built in.[1] [16] Painless Tmux (2018) is a beginner's guide to a terminal multiplexer for anyone who spends part of the day at the command line.[5]
History
The command line began as hardware. The standard terminal of the 1960s was a mechanical teleprinter, introduced in 1963, that worked at about ten characters a second; Modern Terminal Architecture (2026) argues that its slowness shaped the time-sharing operating systems built around it, including buffered input and output and the distinction between foreground and background processes.[10] Video terminals replaced paper, and the VT100 of 1978 was the first popular one whose cursor and screen a program could control through escape sequences, which were standardised the following year. The terminal emulators in use today are programs that imitate that device and its descendants.[11]
The shells form a family, and Learn Bash the Hard Way draws it as a tree. Bash, whose name stands for Bourne-again shell, descends from the Thompson shell and then the Bourne shell; the KornShell is a sibling, tcsh a cousin and the Z shell a child.[2] Bash itself was written in 1989.[4] The book adds a practical moral: different shells exist, they are related and partly compatible, and it is still possible to land on a server that has no Bash at all.[2]
The common ground between shells is written down in POSIX, the standard for Unix-like systems. POSIX Shell Scripting from Scratch is based on the 2017 edition and notes that the 2024 revision left the shell language virtually unchanged.[3] Bash itself keeps changing: version 5.3, released in July 2025, added a form of command substitution that runs in the current shell instead of in a child process.[1]
The command line
Three things that a beginner meets together are separate. The terminal is the window that displays text and accepts typing; the shell is the program running inside it that interprets commands; and the prompt is the shell's signal that it is ready, by custom ending in a dollar sign for an ordinary user and a hash sign for the all-powerful root user.[4] The Hacker Ways explains why the distinction matters: the same local terminal is used to reach other computers, and the shell at the far end is then a remote one.[4] POSIX Shell Scripting from Scratch offers a picture frame and the picture in it as the image.[3]
The first commands are about place. The shell is always in some directory, and on Linux every directory hangs from a single root, with no drive letters; configuration, logs, devices and even running processes appear as files somewhere in the tree.[4] [7] Commands take options introduced by dashes, and the manual pages describe them all; the advice in The Hacker Ways is to know two or three options of a command and to know that the manual is there.[4] Terminal Secrets at Your Fingers (2025) is a reference in that spirit, giving for each command a description, the origin of its name, its usage and its options.[9]
The idea that gives the command line its power is composition. In the account of The Hacker Ways, almost everything a programmer handles is text, each tool does one simple thing very well, and it is by combining tools that problems with no single tool are solved. A pipe connects the output of one program to the input of the next, and redirection sends output to a file.[4]
The case for learning it rests on repetition and reach. Renaming a hundred photographs is one command instead of a hundred rounds of clicking, a weekly backup becomes a script written once, and a remote server often offers nothing but a command line; the same book adds that graphical tools keep their place for exploring and for visual work.[3] The Command Line (2016) and The Unix Command Line (2015) are both addressed to people who find the terminal intimidating, the first to web developers and designers and the second to beginner and intermediate programmers.[21] [22] Windows has a tradition of its own: Commanding Windows (2015), a short introduction that was left unfinished, presents its command interpreter as a descendant of the one in DOS.[12]
Interactive use
Much of a shell's value lies in features that never reach a script. Reliable Bash Scripting lists what belongs to an interactive shell and what a script therefore does not get: the aliases and functions defined in a user's startup file, the command history, job control and a prompt. A script that waits for typed input when nobody is there simply hangs.[1]
The Bash language
Bash is a small language with unusual rules. The books that teach it in depth agree that most bugs come from misunderstanding what the shell does to a command line before the command runs.[1] [2]
Expansion, globbing and quoting
Bash does not execute a line as written. It splits the line into tokens, parses the whole of it, and then applies a fixed sequence of expansions: brace expansion; tilde, variable, arithmetic and command substitution; word splitting; filename expansion; and finally the removal of quotation marks.[1] Filename expansion, or globbing, is why an asterisk given to a command arrives as a list of file names: the shell has replaced it before the command starts.[2] Learn Bash the Hard Way separates globs from regular expressions, which look alike and are different things. A dot has no special meaning in a glob, and files whose names begin with a dot are not matched by a bare asterisk.[2]
Quoting decides which of those steps apply. Inside single quotes nothing is expanded; inside double quotes variables and command substitutions are, but word splitting and globbing are not.[1] The consequence is the commonest fault in shell scripts. An unquoted variable holding a file name with a space in it is split into two words, and a loop over it processes two files that do not exist. The rule in Reliable Bash Scripting is to quote every variable expansion unless there is a specific reason not to, and to keep lists of file names in arrays.[1] Bash for Fun (2022) likewise makes quoting its first chapter.[17]
Variables, subshells and functions
Variables are untyped strings, assigned with no spaces around the equals sign and read with a dollar sign.[1] A variable belongs to the shell that set it. Learn Bash the Hard Way has the reader set one, start a second Bash and find it missing, then export it and find it inherited, which is how environment variables reach every program a shell starts.[2] Arrays exist, with a syntax that needs curly braces to reach an element and gives the programmer little help.[2] Parameter expansion can supply a default for a missing value, stop with an error when a required one is absent, and trim or replace parts of a string without starting another program.[1]
Subshells explain a second family of surprises. Each command in a pipeline runs in a copy of the shell, so a counter incremented inside a loop that reads from a pipe is still zero afterwards; feeding the loop through process substitution keeps it in the current shell.[1] Inside functions, variables should be declared local, because global ones create hidden dependencies between functions.[1]
Several books teach the language from the beginning. Bash programming from scratch (2022) is a programming tutorial for people learning on their own that uses Bash for its examples, with background on how computers and operating systems work.[18] Shellman Bash Scripting (2024) groups its material by task, from archives and arrays to dates, files and HTTP, and Advanced Bash: Told Through Alice In Wonderland presents each topic as an episode of the story in its title, followed by an explanation of the code that appeared in it.[19] [20]
Text processing
Shell scripts spend much of their time on text, and the same few tools recur across the books: grep for finding lines, sed for editing a stream and awk for working on fields. Reliable Bash Scripting gives them two chapters, the second on sed and awk in depth and on when to use which, and recommends putting a regular expression in single quotes so that the shell leaves it alone.[1] POSIX Shell Scripting from Scratch teaches the three together with regular expressions, aimed at parsing log files and tables of data, and The Ultimate Linux Shell Scripting Guide spreads them over five chapters.[3] [15] Linux and Command Line: Your Production Survival Kit counts them among the commands a tester uses daily.[7]
Learn Data with Bash Shell (2020) turns the tools on data. It works through four small data-mining projects on flat files, on the argument that Bash gives an early look at a data set before the analysis proper moves to another language.[23]
Writing reliable scripts
Reliable Bash Scripting opens with a three-line backup script that worked on most nights. One morning the directory it tried to create already existed under a different owner. The command that failed was not checked, the database dump that followed came out truncated and unreadable, and six hours passed before anyone noticed that the backups were missing.[1] The book's point is that shell scripts look like plain instructions chained together, while the language provides few safeguards against common mistakes. It sets six requirements for any script that runs outside a personal sandbox: correctness, portability, security, maintainability, error handling and performance.[1]
Two books build that discipline around a single program. Backup Cookbook with Bash (2026) grows one nightly backup script a guard at a time, each recipe adding a check against a real failure such as a missing drive or a full disk and showing how to prove the check by breaking it on purpose; its starting point is a backup that reported success every night while copying to a drive unplugged weeks before.[24] Zero to Hero Bash (2026), by the same author, builds a backup tool across eight milestones that add strict mode, logging, configuration, argument parsing, checks before the run, error traps and tests.[25] The same instruments recur across the sources: options that stop a script at the first failed command or unset variable, ShellCheck for static analysis and Bats for automated tests.[1] [25]
Portability and POSIX
A script is portable when it runs unchanged on every system it is meant for. POSIX Shell Scripting from Scratch teaches the POSIX subset first and marks every feature that needs Bash. The benefit it gives is scripts that work on Linux, macOS, the BSD systems and the minimal environments inside containers, and it notes that fish, a shell with a syntax of its own, is not compatible.[3] Reliable Bash Scripting names the features that break: the double-bracket test, associative arrays and process substitution all fail under Dash or BusyBox ash, and options peculiar to the GNU version of a tool may be missing elsewhere.[1]
Performance
Every external command a script runs is a new process. Starting one takes under a millisecond on a modern system, but a loop that does so thousands of times can be an order of magnitude slower than one that uses the shell's own features.[1] Pure Bash Bible is built on that observation. It collects functions for trimming strings, handling arrays, reading files and similar jobs that call no external program, on the ground that such calls are expensive and that scripts without them have fewer dependencies.[16] There is a limit in the other direction, and Reliable Bash Scripting ends its performance chapter with the question of when to use a different language.[1]
Automation
Automation is what most scripts are for. Mastering Shell for DevOps (2026) begins with its author's weeks of manual deployments and the change that scripting made, and proceeds through scripts for delivery pipelines, tests, scheduled jobs, the provisioning of infrastructure, logs and containers.[8] Introduction to Bash Scripting ends with worked scripts of the same practical kind: an interactive menu, a command run on several remote servers, a summary of a web server's access logs.[14] The Ultimate Linux Shell Scripting Guide has nearly a hundred labs and looks beyond Bash to the Z shell and PowerShell.[15]
Other books place the shell within a wider job. The Software Developer's Guide to Linux (2024) is written for software engineers and not for system administrators, and gives shell scripts one chapter among seventeen on processes, permissions, remote access, containers and logs.[26] UNIX Unleashed: A Practical Guide for Programming and Shell Scripting (2026) is a textbook for students of computing that runs from the history of Unix and its file system to shell scripting and then to C programs that use system calls.[27] Kubernetes for All: The Course (2026), a course, follows a single Bash tool from its first line to a local Kubernetes platform, and Adversarial Bash (2026) sets a penetration test in the reader's own laboratory under the constraint that every tool used must be one the reader wrote in Bash.[28] [29]
Terminal multiplexers and the terminal
A terminal multiplexer runs several shell sessions inside one terminal. Painless Tmux introduces tmux with a developer who keeps an editor, a rebuild script and a server log in three tabs and can see only one of them. With tmux they sit side by side as panes, each a separate process, and on a remote server the whole arrangement stays alive between connections, so that the work is found as it was left hours or weeks later.[5] Every tmux command begins with a prefix key combination, and the book's first advice is to change the default to one that is easier to reach, in a configuration file it builds up chapter by chapter.[5] The Tao of tmux (2022) goes further into the model, a server that holds sessions, windows and panes, and into configuration, the status line and scripting.[30]
Beneath the shell is the terminal's own protocol. A program controls the screen by writing escape sequences, runs of bytes beginning with the escape character that the terminal treats as commands to move the cursor, clear a region or change a colour on its grid of character cells. Terminal Interfaces (2026) explains them as the foundation for building full-screen terminal programs, and warns that emulators differ in what they support.[11] Modern Terminal Architecture follows a keypress through the kernel's pseudo-terminal devices to the shell and back to pixels on the screen, and ends by building an emulator.[10]
Questions about Bash and Shell Scripting
What is the difference between a terminal and a shell?
Which Bash book should I start with?
A complete beginner can start with POSIX Shell Scripting from Scratch (2026), which requires no command-line or programming experience.[3] Someone who already runs basic commands can take Learn Bash the Hard Way (2019), which requires every exercise to be typed out.[2] People who already write scripts are the audience of Reliable Bash Scripting (2026).[1]
What is the difference between Bash and a POSIX shell?
POSIX is the standard that defines the shell language common to Unix-like systems, and a script written to it runs on any compliant shell, Bash included.[3] Bash adds features of its own, such as the double-bracket test and associative arrays, and scripts that use them fail under Dash or BusyBox ash.[1]
Why should variables be quoted in Bash?
After expanding an unquoted variable, Bash splits its value into words at spaces and expands any wildcard characters in it, so a file name containing a space becomes two words.[1] Double quotes prevent both, and the rule is to quote every variable expansion unless there is a specific reason not to.[1]
What makes a shell script reliable?
Reliable Bash Scripting sets six requirements: correctness, portability, security, maintainability, error handling and performance.[1] It treats them as requirements for any script that runs outside a personal sandbox, because the language provides few safeguards against common mistakes.[1]
Sources
Every source is a Leanpub book or course. The note after each says which part of it was read; where no sample was available, only the public description and table of contents were used.
- 1.Reliable Bash Scripting, Steve Publications: Writing Portable, Secure, and Production-Ready Shell Scripts Across Linux Distributions Free sample: the preface, the introduction with its failed backup script, chapter 1 on how Bash parses and expands a command, and the start of chapter 2 on variables and quoting. 2026. Free sample: PDF · EPUB
- 2.Learn Bash the Hard Way, Ian Miell: Master Bash Using The Only Method That Works Free sample: the foreword and introduction, and the sections on what Bash is, globbing and quoting, and variables. 2019. Free sample: PDF · EPUB
- 3.POSIX Shell Scripting from Scratch, Sultan Zavrak: Write Once, Run Everywhere - A Beginner's Guide Free sample: the welcome section, with the book's structure and its note on POSIX, and the opening of chapter 1 on the command line, the shell and the terminal. 2026. Free sample: PDF
- 4.The Hacker Ways, Juan Reyero: A gentle introduction to the command line and the UNIX toolset. Free sample: the chapter on the terminal, from the prompt and directories to pipes and redirection. 2014. Free sample: PDF · EPUB
- 5.Painless Tmux, Nate Dickson: A Sane Person's Guide to Command Line Happiness Free sample: the preface, the introduction and the first two chapters, on what tmux solves and on the prefix key. The book is marked as a beta. 2018. Free sample: PDF · EPUB
- 6.Really Friendly Command Line Intro, Tracy Osborn: Mac Edition Free sample: the author's introduction, what the command line is, and the first command. 2018. Free sample: PDF · EPUB
- 7.Linux and Command Line: Your Production Survival Kit, Yuri Syuganov: Shell Skills Every QA Engineer Needs for Modern Production Systems Free sample: the introduction and chapter 1 on the Linux directory structure. 2026. Free sample: PDF
- 8.Mastering Shell for DevOps, GitforGits | Asian Publishing House: Automate, streamline, and secure DevOps workflows with modern shell scripting Free sample: the prologue and the table of contents. 2026. Free sample: PDF
- 9.Terminal Secrets at Your Fingers, Ashraf Khodeir Free sample: the list of topics and the start of the first part, on file and directory commands. 2025. Free sample: PDF
- 10.Modern Terminal Architecture, Steve Publications: From TTY and PTY Internals to GPU-Accelerated Rendering Free sample: the contents, the introduction and the start of chapter 1 on teleprinters and video terminals. 2026. Free sample: PDF · EPUB
- 11.Terminal Interfaces, Steve Publications: Building Modern TUI Applications in Go, Python and Rust Free sample: the preface and chapter 1, on terminal history, escape sequences and character cells. 2026. Free sample: PDF · EPUB
- 12.Commanding Windows, Mauricio Fernández: How to get more efficient by using the cmd command line interface. Free sample: the introduction and the first steps at the Windows command prompt. The book is unfinished. 2015. Free sample: PDF · EPUB
- 13.Beginner's Guide to GNU/Linux Scripting, Leonardo Giordani: Learn bash and the Unix core utilities in a relaxed way No sample: from the description and table of contents. 2020.
- 14.Introduction to Bash Scripting, Bobby Iliev No sample: from the description and table of contents. 2023.
- 15.The Ultimate Linux Shell Scripting Guide, Packt Publishing Ltd: Automate, Optimize, and Empower tasks with Linux Shell Scripting No sample: from the description and table of contents. 2025.
- 16.Pure Bash Bible, Dylan Araps: A collection of pure bash alternatives to external processes. No sample: from the description and table of contents. 2018.
- 17.Bash for Fun, Libor Spacek: Bash Programming: Principles and Examples No sample: from the description and table of contents. 2022.
- 18.Bash programming from scratch, Ilya Shpigor No sample: from the description and table of contents. 2022.
- 19.Shellman Bash Scripting, Remisa Yousefvand: How to reduce complexity by right tools and mindset No sample: from the table of contents. 2024.
- 20.Advanced Bash: Told Through Alice In Wonderland, Jonathan Grant No sample: from the description and table of contents.
- 21.The Command Line, Chris Williams: for Web Developers No sample: from the description and table of contents. 2016.
- 22.The Unix Command Line, Just Enough Media: A compassionate guide to the terminal No sample: from the description. 2015.
- 23.Learn Data with Bash Shell, Scientific Programmer: Explore real-world data at the Linux command line No sample: from the description and table of contents. 2020.
- 24.Backup Cookbook with Bash, Muhammad Yaseen Rasheed: A bash cookbook for one nightly backup script that refuses, loudly, instead of failing quietly No sample: from the description and table of contents. 2026.
- 25.Zero to Hero Bash, Muhammad Yaseen Rasheed: Build a Real Backup Tool, One Commit at a Time No sample: from the description and table of contents. 2026.
- 26.The Software Developer's Guide to Linux, Packt Publishing Ltd: A practical, no-nonsense guide to using the Linux command line and utilities as a software developer No sample: from the description and table of contents. 2024.
- 27.UNIX Unleashed: A Practical Guide for Programming and Shell Scripting, Anshuman Mishra No sample: from the description and table of contents. 2026.
- 28.Kubernetes for All: The CourseCourse, Joel Bryan Juliano: Provision, deploy and operate a full Kubernetes platform from scratch with one well-built Bash script. No sample: from the description. 2026.
- 29.Adversarial Bash, Andrew Ingram: Build Your Own Pentest Arsenal — In Bash No sample: from the description and table of contents. 2026.
- 30.The Tao of tmux, Tony Narlock: and Terminal Tricks No sample: from the description and table of contents. 2022.
Further reading
Other books in the library on the same ground, not cited above.
- Shell Samurai, Stetson Blake: Master the Linux Command Line A beginner's introduction to Linux and its command line for people in IT roles. 2023.
- Introduction to Bash Scripting, Shyam Gupta A brief introduction to Bash scripting, from a first script and variables to loops, arrays and debugging.
- Linux Basics for SysAdmin, GitforGits | Asian Publishing House: Learn core linux concepts and command-line skills to kickstart your system administration career An introduction to Linux system administration, with a short section on the shell and Bash scripting. 2026. Free sample: PDF
- Command Line Git, Maksim Ivanov: Everything you need to know to get started A beginner's guide to Git at the command line, with primers on the command line for each operating system. 2023.
- Python Command Line Tools, Noah Gift, Alfredo Deza: Design powerful apps with Click Building command-line tools in Python, with a chapter on writing them in pure Bash or the Z shell. 2020. Free sample: PDF · EPUB
- Mastering Go CLI and TUI, Ravikanth Chaganti: Create powerful, user-friendly command-line (CLI) and terminal user interface (TUI) applications in Go! Writing command-line and full-screen terminal programs in Go, including shell completion for them. 2024.
- Rust CLI Mastery, Rajeev Edmonds: Build Production-Ready Command-Line Tools Building command-line tools in Rust, from argument parsing to release. 2026. Free sample: PDF
- Clap - Command Line Argument Parser for Rust, Gábor Szabó: Accepting parameters on the command line. A short book on accepting command-line arguments in Rust programs.
- Mastering Advanced ADB Command Line, Krzysztof Rybiński A reference on the command-line tool for debugging and automating Android devices. 2026.