#!/bin/sh - Shebang line, specifies the interpreter.
This line should be the first line of the script. It tells the system which interpreter to use to execute the script. Using /bin/sh ensures POSIX compliance.
A quick reference guide to POSIX shell scripting, covering syntax, commands, and best practices for writing portable and robust shell scripts.
|
This line should be the first line of the script. It tells the system which interpreter to use to execute the script. Using |
|
Comments are used to explain the code and are ignored by the interpreter. |
|
Commands are executed sequentially, one per line. Each line typically contains a single command or a control structure. |
|
Semicolons ( Example: |
|
Use A status of |
|
Variable Assignment |
Example: |
|
Variable Access |
Example: |
|
Read-only Variables |
Example: |
|
Unsetting Variables |
Example: |
|
Special Variables |
$0: Script name |
|
Example: |
|
Example: |
|
Example: |
|
Example: |
|
Example:
|
|
Conditions are often enclosed in square brackets Example: |
|
String comparison: |
|
Example:
|
|
Example:
|
|
Example:
|
|
|
|
Example:
|
|
The |
|
|
List directory contents |
|
|
Create a directory |
|
|
Remove a file |
|
|
Remove an empty directory |
|
|
Copy a file |
|
|
Move or rename a file |
|
|
Create an empty file or update its timestamp |
|
|
Search for a pattern in a file |
|
|
Replace text in a file |
|
|
Print the first field of each line in a file |
|
|
Sort the lines in a file |
|
|
Remove duplicate lines from a file |
|
|
Cut out sections of each line of a file |
|
|
List running processes |
|
|
Terminate a process |
|
|
Pause execution for a specified number of seconds |
|
command & |
Run a command in the background |
|
|
Wait for all background processes to complete |
|
Example:
|
|
Functions can accept arguments: Example:
|
|
|
|
Local variables can be declared using Example:
|
|
Example:
|
|
Example:
|
|
Example:
|
|
Common signals: |