Every shell script starts with a shebang line to specify the interpreter:
#!/bin/bash
Followed by comments, variables, and commands.
A quick reference guide to shell scripting, covering essential syntax, commands, and best practices for automating tasks in Unix-like environments.
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: |
|
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. |
Example: |
Example: |
Example: |
Example: |
Example: |
Example:
|
|
Iterates over a list of items. Example:
|
|
Executes commands while a condition is true. Example:
|
|
Executes commands until a condition is true. Example:
|
Example:
|
Example:
|
Passing arguments to functions: |
|
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. |
|
|
|
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
|
|