Linux Shell Scripting Tutorial Part 2 - String Operators

This is Part 2 of Linux Shell Tutorials Series, in this tutorial we will see STRING operators that are commonly used in shell scripts.

For executing these scripts provide Strings as script arguments.

Shell script to check for empty String

  #!/bin/bash

  echo "$1"

  if test -z $1; then
          echo "provided string is  empty"
  fi

Shell script to check for non empty String

  #!/bin/bash

  echo "$1"

  if test -n $1; then
          echo "provided string is not  empty"
  fi


Shell script to check for equal Strings

  #!/bin/bash

  echo "$1 $2"

  if test $1 = $2; then
          echo "provided strings are equal"
  fi


Shell script to check for not equal Strings

  #!/bin/bash

  echo "$1 $2"

  if test $1 != $2; then
          echo "provided strings are not equal"
  fi


Shell script to check if String1 comes before String2 alphabetically

  #!/bin/bash

  echo "$1 $2"

  if test $1 \< $2;then
          echo "$1 comes before $2 alphabetically"
  fi


Shell script to check if String1 comes after String2 alphabetically

  #!/bin/bash

  echo "$1 $2"

  if test $1 \> $2;then
          echo "$1 comes after $2 alphabetically"
  fi



Follow US on Twitter: