Catalog / Bash Scripting Cheatsheet
Bash Scripting Cheatsheet
A concise reference for Bash scripting, covering essential syntax, commands, and best practices.
Bash Basics
Script Structure
|
|
|
Variables
|
Assign a value to a variable. No spaces around |
|
Access the value of a variable. Use |
|
Make a variable read-only. |
|
Unset a variable. |
|
Export a variable to the environment. |
Input/Output
|
Print a message to standard output. |
|
Read input from standard input into a variable. |
|
Display the contents of a file. |
|
Redirect output to a file (overwrite). |
|
Redirect output to a file (append). |
Control Flow
Conditional Statements
|
Example:
|
Looping
For loop:
|
Example:
|
While loop:
|
Example:
|
Until loop:
|
Case Statements
|
Example:
|
Functions
Defining Functions
Or:
|
Defines a function named |
|
Returns a value from the function. The value must be between 0 and 255. |
Calling Functions
|
Calls the function |
|
Access the arguments passed to the function. |
|
Number of arguments passed to the function. |
|
All arguments passed to the function. |
Example Function
|
Command Line Arguments
Accessing Arguments
|
The name of the script. |
|
The first, second, etc., command-line argument. |
|
The number of command-line arguments. |
|
All the command-line arguments as a list. |
|
All the command-line arguments as a single string. |
Argument Parsing
|
A common pattern for parsing command-line arguments. |