In this topic, we've demonstrated bash string and its operators.
Like other programming languages, Bash String may be a data type like an integer or floating-point unit. it's wont to represent text instead of numbers. it's a mixture of a group of characters which will also contain numbers.
For example, the word "Great learning" and therefore the phrase "Welcome to Great learning academy" are the strings. Also "01234" can be counted as a string, if specified correctly. Programmers are required to surround strings in quotation marks for the info to be considered as a string and not variety, variable name or array, etc.
Bash consists of multiple ways to perform string operations and manipulate them.
Following are some operators in Shell Script wont to perform string operations:
Equal Operator
An equal operator (=) is employed to see whether two strings are equal.
Syntax
Operand1 = Operand2
Example 1:
Code/ script to check if the strings are equal or not.
#!/bin/bash
#Script to check whether two strings are equal.
str1="WelcometoGreatlearning."
str2="Greatlearning"
if [ $str1 = $str2 ];
then
echo "Both the strings are equal."
else
echo "Strings are not equal."
fi
Output for the above commands or script
Not equal operator (!=)
This operator will check if two strings are equal or not.
Syntax:
String1 != String2
Example:
Script
#!/bin/bash
#Script to check whether two strings are equal.
str1="WelcometoJavatpoint."
str2="javatpoint"
if [[ $str1 != $str2 ]];
then
echo "Strings are not equal."
else
echo "Strings are equal."
fi
The Output for the above script.
Similarly their some Other commands we can use in strings, which can assist us while dealing with strings.
Here are some other operators.
- Less than Operator (Operand1 \< Operand2)
- Greater than Operator(Operand1 \> Operand2)
- To check if the length is greater than zero or not([-n Operand])