Introduction to Shell Scripting

Shell Scripting A shell script is an executable text file that contains shells commands and other specific programming structures. What is a Shell? A shell is a program that acts as a bridge(interface) between you(user) and the operating system(kernel). It takes commands you type, sends them to the operating system kernel, and then shows you the output. Think of it as a command interpreter. Tip: Use this command to check your current shell ...

September 30, 2025 · 5 min · 947 words · Ahmad Hassan

Shell Expansion in Shell Scripting

Shell Expansion In Section we are going to learn about Shell Expansion, How bash perform the Shell Expansion What is Expansion? When you type a command and press Enter, the shell performs several steps before execution. After tokenization, Command Identification (splitting the command into words/tokens) and parsing, the shell performs expansions. Expansion = the process of replacing symbols, variables, or patterns with their actual values. Example: echo $HOME Here $HOME is expanded into your actual home directory path (like /home/ahmad). ...

September 30, 2025 · 12 min · 2391 words · Ahmad Hassan

Shell Operation in Shell Scripting

Shell Operation in Bash The shell (e.g., bash) follows a multi-step process to interpret and execute commands. Each command goes through reading, breaking down, expanding, redirecting, and finally execution. Steps of Command Processing Read Input From terminal (interactive use) or file/script. Reads line by line. Tokenization (Lexical Analysis) Breaks input into words and operators. Follows specific syntax rules. Alias expansion happens at this stage. Parsing / Command Identification Tokens are recognized as: Simple commands (like ls -l) Compound commands (like if, for, pipelines, etc.) Shell Expansions (performed in order): Brace expansion → {a,b,c} → expands to a b c Tilde expansion → ~ → expands to home directory Parameter & variable expansion → $VAR Command substitution → `command` or $(command) Arithmetic expansion → $((2+3)) → 5 Process substitution → <(command) or >(command) Word splitting → breaks expanded words into fields Filename expansion (globbing) → *.txt → matches files Quote Removal Removes " ' and ` while preserving meaning. Redirections Handles input/output redirection, e.g. > >> < 2>&1. Execution The command is executed. If required, the shell waits for the command to finish. Exit status ($?) is collected. Key Points Tokenization is the first transformation stage. Expansions are ordered; each depends on the result of the previous. Redirection happens before execution. Exit status is important for scripting (conditional logic). Shell Tokenization Definition: The process of breaking input into tokens (smallest meaningful units). A token = sequence of characters treated as a single unit by the shell. How Tokenization Works Input Source From terminal or a file (script). Meta Characters Special unquoted characters that separate words. List of meta characters: Space ( ) Tab (\t) Newline (\n) | & ; ( ) < > Types of Tokens Words: tokens without unquoted meta characters. Operators: tokens containing at least one unquoted meta character. Note: If quoted, they lose their special meaning. ...

September 30, 2025 · 12 min · 2363 words · Ahmad Hassan

Variables in Shell Scripting

Variables in Shell Scripting Variables are placeholders to store data (strings, numbers, paths, etc). They don’t require explicit declaration type (untyped). Stored as strings by default in bash/sh. Creating Variables name="Ahmad" age=20 Note: ⚠️ No spaces around =. Using Variables echo $name echo "I am $age years old" $var → expands value(referencing the value of a variable) ${var} → safer form, avoids ambiguity, and useful for appending file="report" echo "${file}_2025.txt" # report_2025.txt Types of Variables User-defined variables (you create) city="Multan" Environment variables (system-wide, inherited by child processes) echo $PATH export MYVAR="hello" Best Practices ...

September 30, 2025 · 8 min · 1595 words · Ahmad Hassan