Home » Bash String

Bash String

In this topic, we have demonstrated about bash string and its operators.

Like other programming languages, Bash String is a data type such as as an integer or floating-point unit. It is used to represent text rather than numbers. It is a combination of a set of characters that may also contain numbers.

For example, the word “tutoraspire” and the phrase “Welcome to Tutor Aspire” are the strings. Even “01234” could be considered as a string, if specified correctly. Programmers are required to enclose strings in quotation marks for the data to be considered as a string and not a number, variable name or array, etc.

Bash consists of multiple ways to perform string operations and manipulate them.

Following are some operators in Shell Script used to perform string operations:

Equal Operator

An equal operator (=) is used to check whether two strings are equal.

Syntax

Example

Output

Strings are not equal.  

Not Equal Operator

Not equal operator (!=) is used to define that strings are not equal.

Syntax

Example

Output

Strings are not equal.  

Less than Operator

The ‘less than operator (<)’ is a conditional operator which is used to check if string1 is less than string2.

Syntax

Example

Output

Welcometotutoraspire is not less then tutoraspire  

Greater than Operator

The ‘greater than operator (>)` is used to check if string1 is greater than string2.

Syntax

Example

Output

Welcometotutoraspire is greater then tutoraspire  

To check if the string length is greater than Zero:

This operator is used to check if the string is zero or greater than zero.

Syntax

Example

Output

String is not empty  

To check if the string length is equal to Zero

This operator is used to check if the string is empty or equal to zero.

Syntax

Example

Output

String is empty  

Conclusion

In this topic, we have learned about the syntax of different operators in bash scripting with example scripts.


Next TopicBash Find String

You may also like