Skip to content

Instantly share code, notes, and snippets.

@bhumit070
Created July 31, 2024 11:04
Show Gist options
  • Save bhumit070/707c5225eab7728631537e954a4bd439 to your computer and use it in GitHub Desktop.
Save bhumit070/707c5225eab7728631537e954a4bd439 to your computer and use it in GitHub Desktop.
Shell script operator uses

String Comparisons:

-n STRING:

#!/bin/bash

my_string="hello"

if [ -n "$my_string" ]; then
  echo "The string is not empty."
else
  echo "The string is empty."
fi

STRING1 = STRING2:

#!/bin/bash

string1="hello"
string2="hello"

if [ "$string1" = "$string2" ]; then
  echo "The strings are equal."
else
  echo "The strings are not equal."
fi

STRING1 != STRING2:

#!/bin/bash

string1="hello"
string2="world"

if [ "$string1" != "$string2" ]; then
  echo "The strings are not equal."
else
  echo "The strings are equal."
fi

File Checks:

-e FILE:

#!/bin/bash

file="/path/to/file"

if [ -e "$file" ]; then
  echo "The file exists."
else
  echo "The file does not exist."
fi

-f FILE:

#!/bin/bash

file="/path/to/file"

if [ -f "$file" ]; then
  echo "The file exists and is a regular file."
else
  echo "The file does not exist or is not a regular file."
fi

-d FILE:

#!/bin/bash

dir="/path/to/directory"

if [ -d "$dir" ]; then
  echo "The directory exists."
else
  echo "The directory does not exist."
fi

-r FILE:

#!/bin/bash

file="/path/to/file"

if [ -r "$file" ]; then
  echo "The file is readable."
else
  echo "The file is not readable."
fi

-w FILE:

#!/bin/bash

file="/path/to/file"

if [ -w "$file" ]; then
  echo "The file is writable."
else
  echo "The file is not writable."
fi

-x FILE:

#!/bin/bash

file="/path/to/file"

if [ -x "$file" ]; then
  echo "The file is executable."
else
  echo "The file is not executable."
fi

-s FILE:

#!/bin/bash

file="/path/to/file"

if [ -s "$file" ]; then
  echo "The file is not empty."
else
  echo "The file is empty."
fi

Numeric Comparisons:

INTEGER1 -eq INTEGER2:

#!/bin/bash

num1=5
num2=5

if [ "$num1" -eq "$num2" ]; then
  echo "The numbers are equal."
else
  echo "The numbers are not equal."
fi

INTEGER1 -ne INTEGER2:

#!/bin/bash

num1=5
num2=10

if [ "$num1" -ne "$num2" ]; then
  echo "The numbers are not equal."
else
  echo "The numbers are equal."
fi

INTEGER1 -lt INTEGER2:

#!/bin/bash

num1=5
num2=10

if [ "$num1" -lt "$num2" ]; then
  echo "$num1 is less than $num2."
else
  echo "$num1 is not less than $num2."
fi

INTEGER1 -le INTEGER2:

#!/bin/bash

num1=5
num2=10

if [ "$num1" -le "$num2" ]; then
  echo "$num1 is less than or equal to $num2."
else
  echo "$num1 is not less than or equal to $num2."
fi

INTEGER1 -gt INTEGER2:

#!/bin/bash

num1=10
num2=5

if [ "$num1" -gt "$num2" ]; then
  echo "$num1 is greater than $num2."
else
  echo "$num1 is not greater than $num2."
fi

INTEGER1 -ge INTEGER2:

#!/bin/bash

num1=10
num2=5

if [ "$num1" -ge "$num2" ]; then
  echo "$num1 is greater than or equal to $num2."
else
  echo "$num1 is not greater than or equal to $num2."
fi

Logical Operators:

! EXPR:

#!/bin/bash

file="/path/to/file"

if [ ! -e "$file" ]; then
  echo "The file does not exist."
else
  echo "The file exists."
fi

EXPR1 -a EXPR2:

#!/bin/bash

file1="/path/to/file1"
file2="/path/to/file2"

if [ -e "$file1" -a -e "$file2" ]; then
  echo "Both files exist."
else
  echo "Both files do not exist."
fi

EXPR1 -o EXPR2:

#!/bin/bash

file1="/path/to/file1"
file2="/path/to/file2"

if [ -e "$file1" -o -e "$file2" ]; then
  echo "At least one of the files exists."
else
  echo "Neither of the files exists."
fi

These examples cover a range of common tests you might perform in shell scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment