Post

Bash Operators Cheat Sheet

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.

OperatorDescriptionExample

-e

File exists (any type)

[[ -e file.txt ]]

-f

File exists and is a regular file

[[ -f script.sh ]]

-d

File exists and is a directory

[[ -d myfolder ]]

-r

File is readable

[[ -r data.txt ]]

-w

File is writable

[[ -w output.log ]]

-x

File is executable

[[ -x script.sh ]]

-s

File exists and is not empty

[[ -s nonempty.txt ]]

-L

File is a symbolic link

[[ -L shortcut ]]

-b

File is a block device

[[ -b /dev/sda1 ]]

-c

File is a character device

[[ -c /dev/ttyS0 ]]

-p

File is a named pipe (FIFO)

[[ -p pipefile ]]

-S

File is a socket

[[ -S /var/run/docker.sock ]]

2. 2. String Comparison Operators

Used to compare strings in conditional expressions.

OperatorDescriptionExample

-z

String is empty

[[ -z "$var" ]]

-n

String is not empty

[[ -n "$var" ]]

=

Strings are equal

[[ "$a" = "$b" ]]

!=

Strings are not equal

[[ "$a" != "$b" ]]

>

String a is greater than b (lexically)

[[ "$a" > "$b" ]]

<

String a is less than b (lexically)

[[ "$a" < "$b" ]]

3. 3. Numeric Comparison Operators

Used in …​ arithmetic evaluations and [ …​ ] test conditions.

OperatorDescriptionExample

-eq

Equal to

[[ "$a" -eq "$b" ]]

-ne

Not equal to

[[ "$a" -ne "$b" ]]

-lt

Less than

[[ "$a" -lt "$b" ]]

-le

Less than or equal to

[[ "$a" -le "$b" ]]

-gt

Greater than

[[ "$a" -gt "$b" ]]

-ge

Greater than or equal to

[[ "$a" -ge "$b" ]]

4. 4. Logical Operators

Used to combine conditions.

OperatorDescriptionExample

&&

Logical AND (both must be true)

[[ -f file.txt && -r file.txt ]]

||

Logical OR (either can be true)

[[ -d mydir || -f myfile ]]

!

Logical NOT (negation)

[[ ! -e missing.txt ]]

-a

Logical AND (deprecated)

[[ -f file.txt -a -r file.txt ]]

-o

Logical OR (deprecated)

[[ -d mydir -o -f myfile ]]

5. 5. Arithmetic Operators

Used inside …​ for integer calculations.

OperatorDescriptionExample

+

Addition

sum = a + b

-

Subtraction

diff = a - b

*

Multiplication

product = a * b

/

Division

quotient = a / b

%

Modulus (remainder)

remainder = a % b

**

Exponentiation

power = a ** b

++

Increment

a++

--

Decrement

b--

6. 6. Assignment Operators

Used to assign values to variables.

OperatorDescriptionExample

=

Assign value

a=5

+=

Add and assign

a+=2 # a = a + 2

-=

Subtract and assign

b-=3 # b = b - 3

*=

Multiply and assign

c*=4 # c = c * 4

/=

Divide and assign

d/=2 # d = d / 2

%=

Modulus and assign

e%=3 # e = e % 3

**=

Exponentiate and assign

f=2 # f = f 2

7. 7. Bitwise Operators

Used for bitwise manipulation.

OperatorDescriptionExample

&

AND

result = a & b

|

OR

result = a | b

^

XOR

result = a ^ b

~

NOT

result = ~a

<<

Left shift

result = a << 2

>>

Right shift

result = a

8. 8. Process Control Operators

Used to control execution flow.

OperatorDescriptionExample

;

Run multiple commands sequentially

echo "Hello"; echo "World"

&

Run command in the background

sleep 10 &

&&

Run next command if previous succeeds

mkdir test && cd test

`

`

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)

echo "Hello" > file.txt

>>

Redirect output to a file (append)

echo "World" >> file.txt

<

Read input from a file

sort < input.txt

2>

Redirect stderr to a file

command 2> error.log

2>&1

Redirect stderr to stdout

command > output.log 2>&1

&>

Redirect both stdout and stderr to a file

command &> log.txt

$(command)

Command substitution

current_date=$(date)

Arithmetic evaluation

result = a + b

[[ ]]

Advanced test command

[[ -f file.txt ]]

{ }

Group commands in the current shell

{ echo "One"; echo "Two"; }

( )

Group commands in a subshell

(cd /tmp; ls)

9. Conclusion

This cheat sheet provides a quick reference for Bash operators. Mastering these will help you write efficient shell scripts.

This post is licensed under CC BY 4.0 by the author.

Trending Tags