Bash Operators Cheat Sheet
This cheat sheet covers Bash operators including file tests, string comparisons, logical operations, and arithmetic evaluations.
1. 1. File Test Operators
File test operators check the status of files.
Operator | Description | Example |
---|---|---|
| File exists (any type) |
|
| File exists and is a regular file |
|
| File exists and is a directory |
|
| File is readable |
|
| File is writable |
|
| File is executable |
|
| File exists and is not empty |
|
| File is a symbolic link |
|
| File is a block device |
|
| File is a character device |
|
| File is a named pipe (FIFO) |
|
| File is a socket |
|
2. 2. String Comparison Operators
Used to compare strings in conditional expressions.
Operator | Description | Example |
---|---|---|
| String is empty |
|
| String is not empty |
|
| Strings are equal |
|
| Strings are not equal |
|
| String |
|
| String |
|
3. 3. Numeric Comparison Operators
Used in …
arithmetic evaluations and [ … ]
test conditions.
Operator | Description | Example |
---|---|---|
| Equal to |
|
| Not equal to |
|
| Less than |
|
| Less than or equal to |
|
| Greater than |
|
| Greater than or equal to |
|
4. 4. Logical Operators
Used to combine conditions.
Operator | Description | Example |
---|---|---|
&& | Logical AND (both must be true) |
|
| Logical OR (either can be true) |
|
| Logical NOT (negation) |
|
| Logical AND (deprecated) |
|
| Logical OR (deprecated) |
|
5. 5. Arithmetic Operators
Used inside …
for integer calculations.
Operator | Description | Example |
---|---|---|
| Addition |
|
| Subtraction |
|
| Multiplication |
|
| Division |
|
| Modulus (remainder) |
|
| Exponentiation |
|
| Increment |
|
| Decrement |
|
6. 6. Assignment Operators
Used to assign values to variables.
Operator | Description | Example |
---|---|---|
| Assign value |
|
| Add and assign |
|
| Subtract and assign |
|
| Multiply and assign |
|
| Divide and assign |
|
| Modulus and assign |
|
| Exponentiate and assign |
|
7. 7. Bitwise Operators
Used for bitwise manipulation.
Operator | Description | Example |
---|---|---|
& | AND |
|
| OR |
|
| XOR |
|
| NOT |
|
| Left shift |
|
| Right shift |
|
8. 8. Process Control Operators
Used to control execution flow.
Operator | Description | Example |
---|---|---|
| Run multiple commands sequentially |
|
| Run command in the background |
|
| Run next command if previous succeeds |
|
` | ` | |
Run next command if previous fails | `rm file | |
echo "File not found"` | ` | ` |
Pipe output of one command to another | `ls -l | grep ".txt"` |
| Redirect output to a file (overwrite) |
|
| Redirect output to a file (append) |
|
| Read input from a file |
|
| Redirect stderr to a file |
|
| Redirect stderr to stdout |
|
| Redirect both stdout and stderr to a file |
|
| Command substitution |
|
Arithmetic evaluation |
| |
| Advanced test command |
|
| Group commands in the current shell |
|
| Group commands in a subshell |
|