/ _ \ \_\(_)/_/ _//"\\_ more on JOHLEM.net / \ 0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0 ############################################################ # SHELL OPERATORS CHEATSHEET # ############################################################ ============================================================ TABLE OF CONTENTS ============================================================ 1. Overview 2. Basic Operators - Arithmetic Operators - Relational Operators - Logical Operators - String Operators - File Test Operators 3. Command Chaining Operators - Sequential Execution - Conditional Execution 4. Redirection Operators - Input Redirection - Output Redirection - Error Redirection 5. Pipes and Filters 6. Process Control Operators - Background Processes - Job Control 7. Special Operators 8. Comparisons Table 9. Conclusion ============================================================ 1. OVERVIEW ============================================================ Shell operators in Linux are symbols and keywords used for command execution, chaining, testing, or manipulating inputs and outputs in shell scripts or commands. ============================================================ 2. BASIC OPERATORS ============================================================ ### Arithmetic Operators - `+` : Addition (e.g., `expr 5 + 2`) - `-` : Subtraction (e.g., `expr 5 - 2`) - `*` : Multiplication (e.g., `expr 5 \* 2`) - `/` : Division (e.g., `expr 10 / 2`) - `%` : Modulus (e.g., `expr 5 % 2`) ### Relational Operators (used in `[ ]` or `[[ ]]`) - `-eq` : Equal to (e.g., `[ $a -eq $b ]`) - `-ne` : Not equal to - `-gt` : Greater than - `-lt` : Less than - `-ge` : Greater than or equal to - `-le` : Less than or equal to ### Logical Operators - `&&` : Logical AND (e.g., `[ $a -gt 10 ] && echo "True"`) - `||` : Logical OR (e.g., `[ $a -gt 10 ] || echo "False"`) - `!` : Logical NOT (e.g., `! [ $a -eq $b ]`) ### String Operators - `=` : Equal (e.g., `[ "$a" = "$b" ]`) - `!=` : Not Equal - `-z` : String is null - `-n` : String is not null ### File Test Operators - `-e` : File exists - `-f` : File is a regular file - `-d` : Directory exists - `-r` : File is readable - `-w` : File is writable - `-x` : File is executable ============================================================ 3. COMMAND CHAINING OPERATORS ============================================================ ### Sequential Execution - `;` : Execute commands sequentially - Example: `cmd1 ; cmd2` ### Conditional Execution - `&&` : Execute next command only if previous succeeds - Example: `cmd1 && cmd2` - `||` : Execute next command only if previous fails - Example: `cmd1 || cmd2` ============================================================ 4. REDIRECTION OPERATORS ============================================================ ### Input Redirection - `<` : Redirect input from a file - Example: `cmd < input.txt` ### Output Redirection - `>` : Redirect output to a file (overwrite) - Example: `cmd > output.txt` - `>>` : Append output to a file - Example: `cmd >> output.txt` ### Error Redirection - `2>` : Redirect errors to a file - Example: `cmd 2> errors.txt` - `2>>` : Append errors to a file - Example: `cmd 2>> errors.txt` ============================================================ 5. PIPES AND FILTERS ============================================================ - `|` : Pass output of one command as input to another - Example: `cat file.txt | grep "pattern"` ============================================================ 6. PROCESS CONTROL OPERATORS ============================================================ ### Background Processes - `&` : Run command in the background - Example: `cmd &` ### Job Control - `jobs` : List active jobs - `fg` : Bring job to foreground - `bg` : Resume background job ============================================================ 7. SPECIAL OPERATORS ============================================================ - `` `cmd` `` : Command substitution - Example: `output=`date`` - `$(( ))` : Arithmetic expansion - Example: `result=$((5 + 2))` ============================================================ 8. COMPARISONS TABLE ============================================================ | Operator Type | Symbol/Keyword | Example | |----------------------|--------------------|--------------------------------| | Arithmetic | `+`, `-`, `*` | `expr 5 + 2` | | Relational | `-eq`, `-gt` | `[ $a -gt $b ]` | | Logical | `&&`, `||` | `[ $a -gt 10 ] && echo "yes"` | | Redirection | `<`, `>` | `cmd > output.txt` | | Pipe | `|` | `cmd1 | cmd2` | ============================================================ 9. CONCLUSION ============================================================ Shell operators in Linux offer powerful tools for command execution, input/output management, logical tests, and process control. Mastering these operators enhances your scripting skills and command-line efficiency. Use this cheatsheet as a quick reference to boost your productivity! ############################################################