Bash notes operators
- Dominant language
- No language data
- Stars
- 3
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
Bash operators are symbols or keywords that instruct the shell to perform specific actions, such as mathematical calculations, comparisons, or logical evaluations, to control the flow of a script. They are essential for dynamic scripting.
Operators are categorized into several types:
Arithmetic Operators
Used for mathematical calculations within a (( )) or $(( )) construct:
+: Addition
-: Subtraction
*: Multiplication
/: Division
%: Modulus (remainder)
**: Exponentiation
++, --: Increment and decrement operators
Comparison Operators
Used to compare values. Numeric comparisons use specific operators within [[ ]] or [ ] (or with the test command), while string comparisons use different ones:
Numeric Comparison (e.g., [ $a -eq $b ])
-eq: Equal to
-ne: Not equal to
-lt: Less than
-le: Less than or equal to
-gt: Greater than
-ge: Greater than or equal to
String Comparison (e.g., [[ "$a" = "$b" ]])
=: Equal to (use for POSIX compatibility)
==: Equal to (Bash specific)
!=: Not equal to
<: Less than (ASCII alphabetical order)
>: Greater than (ASCII alphabetical order)
=~: Regular expression match operator (only within [[ ]])
Logical Operators
Combine multiple conditional expressions:
&&: Logical AND. Executes the second command/expression only if the first succeeds (returns an exit code of 0).
||: Logical OR. Executes the second command/expression only if the first fails (returns a non-zero exit code).
!: Logical NOT. Inverts the result of a condition.
File Test Operators
Check various characteristics of files and directories (e.g., [ -e filename ] checks if a file exists):
-e: Checks if the file exists.
-f: Checks if it is a regular file.
-d: Checks if it is a directory.
-r, -w, -x: Checks for read, write, or execute permissions.
-s: Checks if the file is not empty.
Input/Output Redirection and Piping
Control the flow of data between commands and files:
>: Redirects standard output (stdout) to a file, overwriting the file if it exists.
>>: Appends stdout to a file.
<: Redirects standard input (stdin) from a file.
|: Pipes the stdout of one command to the stdin of the next command.
<<<: "Here String"; supplies a word/variable's content as standard input to a command.
$(...) or `...`: Command substitution; the output of the enclosed command replaces the expression itself.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.