Bash Regular Expression Cheat Sheet



For use in [ ]if [ ]; then and test.

  1. Bash Regular Expression Cheat Sheet
  2. Bash Cheat Sheet
  3. Bash Regular Expression List
  4. Bash Regular Expression Examples
  5. 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 [.

Bash

Variable conditionals

Use these to check if a variable is empty or non-empty.

ExpressionValueDescription
-nvarIf the length of string is non-zero.
-zvarIf the length of string is zero.

Variable comparisons

String:

ExpressionDescription
var1 = var2Equal to.
var1 != var2Not equal to.

Note: Use instead of = inside of [[ for bash, zsh scripts.

Numeric:

ExpressionDescription
var1 -eq var2Equal to.
var1 -ne var2Not equal to.
var1 -gt var2Greater than.
var1 -ge var2Greater than or equal to.
var1 -lt var2Less than.
var1 -le var2Less than or equal to.

File conditionals

Common:

ExpressionValueDescription
-efileIf file exists and is any type.
-ffileIf file exists and is a regular file.
-dfileIf file exists and is a directory.
-h/-LfileIf file exists and is a symbolic link.
-rfileIf file exists and is readable.
-wfileIf file exists and is writable.
-xfileIf file exists and is executable.
-sfileIf file exists and has non-zero size (is non-empty).

Bash Regular Expression Cheat Sheet

Rare:

ExpressionValueDescription
-bfileIf file exists and is a block special file.
-cfileIf file exists and is a character special file.
-gfileIf file exists and its set-group-id bit is set.
-pfileIf file exists and is a named pipe (FIFO).
-tfdIf file descriptor is open and refers to a terminal.
-ufileIf file exists and its set-user-id bit is set.
-SfileIf file exists and is a socket.

Assignment

OperatorDescription
=Initialize or change the value of a variable.

Logical

Bash Cheat Sheet

OperatorDescription
!NOT
&&AND
||OR

Arithmetic

OperatorDescription
+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

ParameterDescription
${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

ParameterDescription
${#VAR}Length of var in characters.

Default Value

ParameterDescription
${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.

SequenceDescriptionValue
033[38;5;<NUM>mSet text foreground color.0-255
033[48;5;<NUM>mSet text background color.0-255
033[38;2;<R>;<G>;<B>mSet text foreground color to RGB color.R, G, B
033[48;2;<R>;<G>;<B>mSet text background color to RGB color.R, G, B

Text Attributes

SequenceDescription
033[mReset text formatting and colors.
033[1mBold text.
033[2mFaint text.
033[3mItalic text.
033[4mUnderline text.
033[5mSlow blink.
033[7mSwap foreground and background colors.
033[8mHidden text.
033[9mStrike-through text.
VariableDescription
$-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.

ExpressionValueDescription
-nvarIf the length of string is non-zero.
-zvarIf the length of string is zero.

Variable comparisons

String:

ExpressionDescription
var1 = var2Equal to.
var1 != var2Not equal to.

Note: Use instead of = inside of [[ for bash, zsh scripts.

Numeric:

ExpressionDescription
var1 -eq var2Equal to.
var1 -ne var2Not equal to.
var1 -gt var2Greater than.
var1 -ge var2Greater than or equal to.
var1 -lt var2Less than.
var1 -le var2Less than or equal to.

File conditionals

Bash regular expression examples

Common:

ExpressionValueDescription
-efileIf file exists and is any type.
-ffileIf file exists and is a regular file.
-dfileIf file exists and is a directory.
-h/-LfileIf file exists and is a symbolic link.
-rfileIf file exists and is readable.
-wfileIf file exists and is writable.
-xfileIf file exists and is executable.
-sfileIf file exists and has non-zero size (is non-empty).

Rare:

ExpressionValueDescription
-bfileIf file exists and is a block special file.
-cfileIf file exists and is a character special file.
-gfileIf file exists and its set-group-id bit is set.
-pfileIf file exists and is a named pipe (FIFO).
-tfdIf file descriptor is open and refers to a terminal.
-ufileIf file exists and its set-user-id bit is set.
-SfileIf file exists and is a socket.

Assignment

OperatorDescription
=Initialize or change the value of a variable.

Logical

OperatorDescription
!NOT
&&AND
||OR

Arithmetic

OperatorDescription
+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

ParameterDescription
${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

ParameterDescription
${#VAR}Length of var in characters.

Default Value

ParameterDescription
${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.

SequenceDescriptionValue
033[38;5;<NUM>mSet text foreground color.0-255
033[48;5;<NUM>mSet text background color.0-255
033[38;2;<R>;<G>;<B>mSet text foreground color to RGB color.R, G, B
033[48;2;<R>;<G>;<B>mSet text background color to RGB color.R, G, B

Text Attributes

SequenceDescription
033[mReset text formatting and colors.
033[1mBold text.
033[2mFaint text.
033[3mItalic text.
033[4mUnderline text.
033[5mSlow blink.
033[7mSwap foreground and background colors.
033[8mHidden text.
033[9mStrike-through text.
VariableDescription
$-Shell options
$$Current shell PID