
For use in [ ]
if [ ]; then
and test
.
- Bash Regular Expression Cheat Sheet
- Bash Cheat Sheet
- Bash Regular Expression List
- Bash Regular Expression Examples
- Bash Regular Expression Replace
Man$command’:$displaythecommand’s(manualpage( JacquesDainat(5(2015(BASH cheat sheet - $Level 2 Wildcards Miscellaneous ’ (Escape(character.Itpreserves(the. With bash, an expression is normally enclosed using and can use the following operators, in order of precedence. » Bash Cheat Sheet Johns Blog Page 4 of 4. Bash Regex Cheat Sheet Edit Cheat Sheet Regexp Matching. Use conditions with doubled and the = operator. Ensure not to quote the regular expression. Only BRE are allowed. If the regexp has whitespaces put it in a variable first.
Note: When writing a bash or zsh script, use [[
instead of POSIX [
.

Variable conditionals
Use these to check if a variable is empty or non-empty.
Expression | Value | Description |
---|
-n | var | If the length of string is non-zero. |
-z | var | If the length of string is zero. |
Variable comparisons
String:
Expression | Description |
---|
var1 = var2 | Equal to. |
var1 != var2 | Not equal to. |
Note: Use instead of =
inside of [[
for bash, zsh scripts.
Numeric:
Expression | Description |
---|
var1 -eq var2 | Equal to. |
var1 -ne var2 | Not equal to. |
var1 -gt var2 | Greater than. |
var1 -ge var2 | Greater than or equal to. |
var1 -lt var2 | Less than. |
var1 -le var2 | Less than or equal to. |
File conditionals
Common:
Expression | Value | Description |
---|
-e | file | If file exists and is any type. |
-f | file | If file exists and is a regular file. |
-d | file | If file exists and is a directory. |
-h /-L | file | If file exists and is a symbolic link. |
-r | file | If file exists and is readable. |
-w | file | If file exists and is writable. |
-x | file | If file exists and is executable. |
-s | file | If file exists and has non-zero size (is non-empty). |
Bash Regular Expression Cheat Sheet
Rare:
Expression | Value | Description |
---|
-b | file | If file exists and is a block special file. |
-c | file | If file exists and is a character special file. |
-g | file | If file exists and its set-group-id bit is set. |
-p | file | If file exists and is a named pipe (FIFO). |
-t | fd | If file descriptor is open and refers to a terminal. |
-u | file | If file exists and its set-user-id bit is set. |
-S | file | If file exists and is a socket. |
Assignment
Operator | Description |
---|
= | Initialize or change the value of a variable. |
Logical
Bash Cheat Sheet
Operator | Description |
---|
! | NOT |
&& | AND |
|| | OR |
Arithmetic
Operator | Description |
---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
** | Exponentiation |
% | Modulo |
+= | Plus-Equal |
-= | Minus-Equal |
*= | Times-Equal |
/= | Slash-Equal |
%= | Mod-Equal |
Use these in place of awk
or sed
calls when possible.
Replacement
Parameter | Description |
---|
${VAR//PATTERN/REPLACE} | Substitute pattern with replacement. |
${VAR#PATTERN} | Remove shortest match of pattern from start. |
${VAR##PATTERN} | Remove longest match of pattern from start. |
${VAR%PATTERN} | Remove shortest match of pattern from end. |
${VAR%%PATTERN} | Remove longest match of pattern from end. |
Length
Parameter | Description |
---|
${#VAR} | Length of var in characters. |
Default Value
Parameter | Description |
---|
${VAR:-STRING} | If VAR is empty or unset, use STRING as its value. |
${VAR-STRING} | If VAR is unset, use STRING as its value. |
${VAR:=STRING} | If VAR is empty or unset, set the value of VAR to STRING . |
${VAR=STRING} | If VAR is unset, set the value of VAR to STRING . |
${VAR:+STRING} | If VAR is not empty, use STRING as its value. |
${VAR+STRING} | If VAR is set, use STRING as its value. |
${VAR:?STRING} | Display an error if empty or unset. |
${VAR?STRING} | Display an error if unset. |
Text Colors
Note: Sequences using RGB values only work in 24-bit true-color mode.
Sequence | Description | Value |
---|
033[38;5;<NUM>m | Set text foreground color. | 0-255 |
033[48;5;<NUM>m | Set text background color. | 0-255 |
033[38;2;<R>;<G>;<B>m | Set text foreground color to RGB color. | R , G , B |
033[48;2;<R>;<G>;<B>m | Set text background color to RGB color. | R , G , B |
Text Attributes
Sequence | Description |
---|
033[m | Reset text formatting and colors. |
033[1m | Bold text. |
033[2m | Faint text. |
033[3m | Italic text. |
033[4m | Underline text. |
033[5m | Slow blink. |
033[7m | Swap foreground and background colors. |
033[8m | Hidden text. |
033[9m | Strike-through text. |
Variable | Description |
---|
$- | Shell options |
$$ | Current shell PID |
For use in [ ]
if [ ]; then
and test
.
Note: When writing a bash or zsh script, use [[
instead of POSIX [
.
Variable conditionals
Bash Regular Expression List
Use these to check if a variable is empty or non-empty.
Expression | Value | Description |
---|
-n | var | If the length of string is non-zero. |
-z | var | If the length of string is zero. |
Variable comparisons
String:
Expression | Description |
---|
var1 = var2 | Equal to. |
var1 != var2 | Not equal to. |
Note: Use instead of =
inside of [[
for bash, zsh scripts.
Numeric:
Expression | Description |
---|
var1 -eq var2 | Equal to. |
var1 -ne var2 | Not equal to. |
var1 -gt var2 | Greater than. |
var1 -ge var2 | Greater than or equal to. |
var1 -lt var2 | Less than. |
var1 -le var2 | Less than or equal to. |
File conditionals

Common:
Expression | Value | Description |
---|
-e | file | If file exists and is any type. |
-f | file | If file exists and is a regular file. |
-d | file | If file exists and is a directory. |
-h /-L | file | If file exists and is a symbolic link. |
-r | file | If file exists and is readable. |
-w | file | If file exists and is writable. |
-x | file | If file exists and is executable. |
-s | file | If file exists and has non-zero size (is non-empty). |
Rare:
Expression | Value | Description |
---|
-b | file | If file exists and is a block special file. |
-c | file | If file exists and is a character special file. |
-g | file | If file exists and its set-group-id bit is set. |
-p | file | If file exists and is a named pipe (FIFO). |
-t | fd | If file descriptor is open and refers to a terminal. |
-u | file | If file exists and its set-user-id bit is set. |
-S | file | If file exists and is a socket. |
Assignment
Operator | Description |
---|
= | Initialize or change the value of a variable. |
Logical
Operator | Description |
---|
! | NOT |
&& | AND |
|| | OR |
Arithmetic
Operator | Description |
---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
** | Exponentiation |
% | Modulo |
+= | Plus-Equal |
-= | Minus-Equal |
*= | Times-Equal |
/= | Slash-Equal |
%= | Mod-Equal |
Use these in place of awk
or sed
calls when possible.
Replacement
Parameter | Description |
---|
${VAR//PATTERN/REPLACE} | Substitute pattern with replacement. |
${VAR#PATTERN} | Remove shortest match of pattern from start. |
${VAR##PATTERN} | Remove longest match of pattern from start. |
${VAR%PATTERN} | Remove shortest match of pattern from end. |
${VAR%%PATTERN} | Remove longest match of pattern from end. |
Length
Parameter | Description |
---|
${#VAR} | Length of var in characters. |
Default Value
Parameter | Description |
---|
${VAR:-STRING} | If VAR is empty or unset, use STRING as its value. |
${VAR-STRING} | If VAR is unset, use STRING as its value. |
${VAR:=STRING} | If VAR is empty or unset, set the value of VAR to STRING . |
${VAR=STRING} | If VAR is unset, set the value of VAR to STRING . |
${VAR:+STRING} | If VAR is not empty, use STRING as its value. |
${VAR+STRING} | If VAR is set, use STRING as its value. |
${VAR:?STRING} | Display an error if empty or unset. |
${VAR?STRING} | Display an error if unset. |
Bash Regular Expression Examples
Text Colors
Bash Regular Expression Replace
Note: Sequences using RGB values only work in 24-bit true-color mode.
Sequence | Description | Value |
---|
033[38;5;<NUM>m | Set text foreground color. | 0-255 |
033[48;5;<NUM>m | Set text background color. | 0-255 |
033[38;2;<R>;<G>;<B>m | Set text foreground color to RGB color. | R , G , B |
033[48;2;<R>;<G>;<B>m | Set text background color to RGB color. | R , G , B |
Text Attributes
Sequence | Description |
---|
033[m | Reset text formatting and colors. |
033[1m | Bold text. |
033[2m | Faint text. |
033[3m | Italic text. |
033[4m | Underline text. |
033[5m | Slow blink. |
033[7m | Swap foreground and background colors. |
033[8m | Hidden text. |
033[9m | Strike-through text. |
Variable | Description |
---|
$- | Shell options |
$$ | Current shell PID |