Master Shell Basics: From What a Shell Is to Advanced Variable Tricks
This comprehensive guide explains what a shell and shell script are, compares common shells, shows how to write and run scripts, explores variable types, special and environment variables, and demonstrates powerful string manipulation and batch‑renaming techniques for Linux users.
What is a Shell?
Shell is a command interpreter that wraps the OS kernel, allowing users to interact with the system by typing commands which the shell parses and executes.
Common Shell Types
Bash (Bourne Again Shell) – default on most Linux and macOS.
Sh (Bourne Shell) – original Unix shell.
Tcsh (C Shell) – C‑like syntax.
Zsh – advanced features, default on macOS Catalina and later.
Fish – user‑friendly with autosuggestions.
What is a Shell Script?
A shell script is a text file containing a sequence of shell commands and control structures that can be executed non‑interactively.
Shebang
The first line #!/bin/bash tells the system which interpreter to use.
Executing Scripts
Run with bash script.sh or sh script.sh (no execute permission needed).
Make executable ( chmod +x script.sh) and run ./script.sh or by absolute path.
Variables in Shell
Variables are defined without spaces ( name="value") and accessed with $name or ${name}. Variable names may contain letters, numbers, and underscores but cannot start with a digit.
System Variables
$HOME, $PWD, $SHELL, $USER and similar built‑in variables.
Special Variables
$?– exit status of the last command (0 for success, 1‑255 for error). $# – number of positional parameters passed to a script or function. $* – all positional parameters as a single word (when quoted). $@ – all positional parameters as separate words (when quoted).
Environment Variables
Exported with export VAR=value and stored in configuration files such as ~/.bashrc, ~/.bash_profile, or global files like /etc/profile and /etc/profile.d/*.sh.
String Substitution and Substring Extraction
Examples of powerful parameter expansion: ${var:2:5} – extract a substring starting at offset 2 with length 5. ${var#pattern} – remove the shortest matching prefix. ${var##pattern} – remove the longest matching prefix. ${var%pattern} – remove the shortest matching suffix. ${var%%pattern} – remove the longest matching suffix. ${var/pattern/repl} – replace the first occurrence of pattern with repl. ${var//pattern/repl} – replace all occurrences.
Practical Example: Batch Rename Files
for f in *finished.jpg; do
mv "$f" "${f//_finished/}"
doneConclusion
Shell scripting is a lightweight, powerful tool for system automation, file manipulation, and quick prototyping, making it essential for developers and operations engineers.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
