Shell scripting relies on a variety of symbols and operators that control the flow of commands, handle input/output redirection, and perform logical operations. Understanding these symbols is crucial for both beginners and advanced users to automate tasks effectively. Symbols like -d and -f check for directories and files, while comparison operators such as -eq, -lt, and -gt help manage conditional logic. Redirection symbols like >, >>, and | allow you to control how data flows between commands and files, offering powerful ways to process and manipulate data.
In addition, shell scripts use special symbols such as $$ to access the current process ID and $? to check the status of the last executed command, aiding in error handling and debugging. Logical operators like && and || are essential for chaining commands based on success or failure. By mastering these common shell script symbols and their use cases, users can build efficient and flexible scripts to automate a wide range of tasks, from system administration to software deployment.
| Symbol | Description |
|---|---|
-d | Checks if a directory exists |
| |
-f | Checks if a file exists |
| |
-eq, -ne, -lt, -le, -gt, -ge | Numerical comparison operators: -eq (equal), -ne (not equal), -lt (less than), -le (less than or equal), -gt (greater than), -ge (greater than or equal) |
| |
$? | Returns the exit status of the last command (0 for success, non-zero for failure) |
| |
$# | Returns the number of arguments passed to a script |
| |
$$ | Returns the process ID of the current shell |
| |
$! | Returns the process ID of the last background process |
| |
&& | Logical AND: Executes the next command only if the previous one was successful |
| |
|| | Logical OR: Executes the next command only if the previous one failed |
| |
> | Redirects output to a file (overwrites) |
| |
>> | Appends output to a file |
| |
< | Redirects input from a file |
| |
<< | Here document: Reads input from the current script until a given delimiter |
| |
| | Pipes the output of one command to another command |
| |
* | Matches all files or acts as a wildcard in patterns |
| |
? | Matches any single character in a filename |
| |
; | Separates multiple commands on a single line |
| |
# | Marks a comment line |
| |