Catalog / Shell Scripting Cheatsheet
Shell Scripting Cheatsheet
A quick reference guide to shell scripting, covering essential syntax, commands, and best practices for automating tasks in Unix-like environments.
Basic Syntax & Structure
Script Structure
Every shell script starts with a shebang line to specify the interpreter: Followed by comments, variables, and commands. |
Comments are denoted by Example: |
Variables are assigned using Example: |
Variables
|
Accessing a variable’s value. Example: |
|
Name of the script. |
|
Arguments passed to the script. |
|
Number of arguments passed to the script. |
|
All arguments as a single string or separate words. |
|
Exit status of the last executed command. |
Input/Output
Example: |
Example: |
Example: |
Example: |
Example: |
Control Flow
Conditional Statements
Example:
|
Looping
|
Iterates over a list of items. Example:
|
|
Executes commands while a condition is true. Example:
|
|
Executes commands until a condition is true. Example:
|
Case Statements
Example:
|
Functions & Commands
Function Definition
Example:
|
Passing arguments to functions: |
Essential Commands
|
List directory contents. |
|
Change directory. |
|
Create directory. |
|
Remove files or directories. |
|
Copy files or directories. |
|
Move files or directories. |
|
Concatenate and display files. |
|
Search for patterns in files. |
|
Search for files based on criteria. |
String Manipulation
|
|
|
Advanced Techniques
Error Handling
Example:
|
Example:
|
Example:
|
Process Substitution
Example:
|
Example:
|
Debugging
|
|