Skip to content

Instantly share code, notes, and snippets.

@ayuxsec
Last active February 28, 2026 14:08
Show Gist options
  • Select an option

  • Save ayuxsec/32ede0c97deec5b911e0a51f85c2957a to your computer and use it in GitHub Desktop.

Select an option

Save ayuxsec/32ede0c97deec5b911e0a51f85c2957a to your computer and use it in GitHub Desktop.
[ "$#" -t 0 ] && echo "Not piped"
# ───────── STRING TESTS ─────────
[ -z "$var" ] # true if string is empty or unset
[ -n "$var" ] # true if string is non-empty
[ "$a" = "$b" ] # true if strings are equal
[ "$a" != "$b" ] # true if strings are not equal
[ "$a" \< "$b" ] # true if a sorts before b (lexicographically)
[ "$a" \> "$b" ] # true if a sorts after b
# ───────── NUMERIC TESTS ─────────
[ "$a" -eq "$b" ] # equal
[ "$a" -ne "$b" ] # not equal
[ "$a" -lt "$b" ] # less than
[ "$a" -le "$b" ] # less than or equal
[ "$a" -gt "$b" ] # greater than
[ "$a" -ge "$b" ] # greater than or equal
# ───────── FILE TESTS ─────────
[ -e "$file" ] # file exists
[ -f "$file" ] # regular file
[ -d "$file" ] # directory
[ -L "$file" ] # symbolic link
[ -p "$file" ] # named pipe
[ -S "$file" ] # socket
[ -b "$file" ] # block device
[ -c "$file" ] # character device
[ -r "$file" ] # readable
[ -w "$file" ] # writable
[ -x "$file" ] # executable
[ -s "$file" ] # file exists and is not empty
[ -O "$file" ] # owned by current user
[ -G "$file" ] # owned by current group
[ -u "$file" ] # setuid bit set
[ -g "$file" ] # setgid bit set
[ -k "$file" ] # sticky bit set
[ "$f1" -nt "$f2" ] # f1 newer than f2
[ "$f1" -ot "$f2" ] # f1 older than f2
[ "$f1" -ef "$f2" ] # same inode
# ───────── VARIABLE / PARAMETER TESTS ─────────
[ -v var ] # variable var is set (bash ≥4.2)
[ -R var ] # variable is a nameref
[ -t 0 ] # stdin is a terminal
[ -t 1 ] # stdout is a terminal
# ───────── LOGICAL OPERATORS ─────────
[ cond1 ] && [ cond2 ] # AND
[ cond1 ] || [ cond2 ] # OR
[ ! cond ] # NOT
# ───────── ARITHMETIC ((( ))) ─────────
(( a == b )) # equal
(( a != b )) # not equal
(( a < b )) # less than
(( a <= b )) # less than or equal
(( a > b )) # greater than
(( a >= b )) # greater than or equal
(( a )) # true if non-zero
(( !a )) # true if zero
# ───────── [[ ]] EXTENDED TESTS (BASH) ─────────
[[ -z $var ]] # empty
[[ -n $var ]] # non-empty
[[ $a == $b ]] # string equal
[[ $a != $b ]] # string not equal
[[ $a < $b ]] # lexicographically less
[[ $a > $b ]] # lexicographically greater
[[ $var == *.txt ]] # glob match
[[ $var =~ regex ]] # regex match
[[ -e $file ]] # file exists
[[ -f $file ]] # regular file
[[ -d $file ]] # directory
# ───────── COMMAND STATUS ─────────
command # run command
[ $? -eq 0 ] # previous command succeeded
[ $# != 4 ] # compare number of args passed
command && echo ok # success
command || echo fail # failure
# ───────── EXIT STATUS SHORTCUTS ─────────
true # always true (exit 0)
false # always false (exit 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment