UNIX / Linux-Shell Basic Operators- Shell String Operators Example - Unix/Linux

What are UNIX / Linux Shell String Operators Example?

The following string operators are supported by Bourne Shell.

Assume variable a holds "abc" and variable b holds "efg" then −

Operator Description Example
= Checks if the value of two operands are equal or not; if yes, then the condition becomes true. [ $a = $b ] is not true.
!= Checks if the value of two operands are equal or not; if values are not equal then the condition becomes true. [ $a != $b ] is true.
-z Checks if the given string operand size is zero; if it is zero length, then it returns true. [ -z $a ] is not true.
-n Checks if the given string operand size is non-zero; if it is nonzero length, then it returns true. [ -n $a ] is not false.
str Checks ifstris not the empty string; if it is empty, then it returns false. [ $a ] is not false.

Example

Here is an example which uses all the string operators −

The above script will generate the following result −

The following points need to be considered while using the operator −

  • There must be spaces between the operators and the expressions. For example, 2+2 is not correct. It should be written as 2 + 2.
  • if...then...else...fi statement is a decision-making statement which has been explained in the next chapter.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Unix/Linux Topics