Shebang:
#!/usr/bin/env bash
This line specifies the interpreter for the script.
A quick reference for Bash scripting, covering essential syntax, commands, and best practices for writing efficient and robust scripts.
Shebang:
This line specifies the interpreter for the script. |
Variables:
Variable assignment and usage. Always quote variables unless you need wildcard expansion or command substitution. |
String Quotes:
Double quotes allow variable interpolation, single quotes do not. |
Command Substitution:
Executes a command and inserts its output. |
Conditional Execution:
Execute commands based on the success or failure of previous commands. |
Strict Mode:
Enable strict mode to catch errors and improve script reliability. |
Single-line comment:
|
Multi-line comment:
|
|
Same as |
|
Same as |
|
Same as |
|
Same as |
Basics:
|
More basics:
|
Path manipulation:
|
More path manipulation:
|
More string manipulation:
|
|
Remove suffix |
|
Remove prefix |
|
Remove long suffix |
|
Remove long prefix |
|
Replace first match |
|
Replace all |
Case conversion:
|
More case conversion:
|
|
|
|
Set |
|
|
|
Show error message and exit if |
Basic
|
C-style
|
Ranges in
|
Ranges with step size:
|
Reading lines from a file:
|
Infinite loop:
|
Defining a function:
Alternative syntax:
|
Calling a function:
|
Returning values:
|
Raising errors:
|
Function Arguments:
|
|
Number of arguments |
|
All positional arguments (as a single word) |
|
All positional arguments (as separate strings) |
|
First argument |
|
Last argument of the previous command |
String conditions:
|
Numeric conditions:
|
File conditions:
|
|
Empty string |
|
Not empty string |
|
Equal |
|
Not Equal |
|
Equal |
|
Not equal |
Defining an array:
|
Accessing array elements:
|
Array operations:
|
Iterate array:
|
Defining a dictionary:
|
Accessing dictionary elements:
|
Dictionary operations:
|
Iterating dictionary (values):
|
Iterating dictionary (keys):
|
Setting options:
|
Glob options:
|
Commands:
|
Expansions:
|
Operations:
|
Numeric calculations:
|
Redirection:
|
Inspecting commands:
|
Case/switch statment:
|
Go to the previous directory:
|
|
Exit status of last task |
|
PID of last background task |
|
PID of shell |
|
Filename of the shell script |
|
Last argument of the previous command |