Every bash script starts with a shebang line, specifying the interpreter.
#!/usr/bin/env bash
A concise reference for Bash scripting, covering essential syntax, commands, and best practices. Includes variables, loops, conditionals, functions, and more, to help you write effective and maintainable scripts.
Every bash script starts with a shebang line, specifying the interpreter.
|
Use comments to explain your code.
|
Best practice: use strict mode to catch errors early.
|
Defining a variable: |
|
Accessing a variable: |
|
String quotes: |
|
Variable scope (local): |
|
Readonly variables: |
|
Generate combinations of strings.
|
Basic
|
C-style
|
Looping through a range:
|
Looping with step size:
|
Reading lines from a file:
|
Infinite loop:
|
Basic |
|
Using |
|
Numeric conditions: |
|
File existence check: |
|
|
Defining a function: |
|
Calling a function: |
|
Returning values: |
|
Raising errors: |
|
Passing arguments: |
|
Defining arrays:
|
Accessing elements:
|
Iterating through elements:
|
Getting array length:
|
Adding elements:
|
Basics:
|
String manipulation:
|
Default values:
|
Stdout to file: |
|
Stdout to file (append): |
|
Stderr to file: |
|
Stderr to stdout: |
|
Both stdout and stderr to file: |
|
Feed file to stdin: |
|
Show history:
|
Expand last parameter of most recent command:
|
Expand all parameters of most recent command:
|
Expand nth most recent command:
|
Execute last command again:
|
Replace in last command:
|
Disable file overwriting: |
|
Exit on error: |
|
Fail on pipe errors: |
|
Unset variables are errors: |
|
Wildcards match dotfiles: |
|
Recursive globbing |
|
Uppercase and Lowercase Transformations:
|
Transforming strings using
|
Getting the directory of the script:
|