{"id":18461,"date":"2022-09-17T05:18:49","date_gmt":"2022-09-17T05:18:49","guid":{"rendered":"https:\/\/linuxways.net\/?p=18461"},"modified":"2022-09-20T14:22:49","modified_gmt":"2022-09-20T14:22:49","slug":"bash-script-string-comparison-tutorial","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/bash-programming\/bash-script-string-comparison-tutorial\/","title":{"rendered":"Bash Script String Comparison Tutorial"},"content":{"rendered":"<p>The Bash scripts also work with the string, so it is possible to compare the string in bash. We will see the comparison of the strings in Bash scripting, especially with the Bash scripts if\/else method. The string is a combination of characters. The strings are identical if they have the same characters in the same order. We need to check whether the strings are equal or needed for this, we have to compare the bash strings. The strings are compared with the comparison operator and bash supports almost all the comparison operators.<\/p>\n<h2><strong>Example 1: Bash String Comparison with \u201c=\u201d Operator in Ubuntu 22.04<\/strong><\/h2>\n<p>We have an equality \u201c=\u201d operator in bash for comparison of the string. The \u201c=\u201d operator determines the given bash strings are equal. We will use the \u201c=\u201d operator in the if-else statement for the string comparison in bash.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"674\" height=\"231\" class=\"wp-image-18464\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-1.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-1.jpeg 674w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-1-300x103.jpeg 300w\" sizes=\"auto, (max-width: 674px) 100vw, 674px\" \/><\/p>\n<p>We have created the bash file where we have implemented the bash program to compare two strings. First, we have defined the attributes \u201cstr1\u201d and \u201cstr2\u201d which are initialized with the different strings. The strings will be printed as we have used with the echo commands. Then, we utilized the if-else condition for string comparison. The if statement is employed with the \u201c=\u201d operator. The \u201c=\u201d operator is used between the \u201cstr1\u201d and \u201cstr2\u201d to determine if the specified strings are equal or not. After the comparison of the bash string, the echo statements from the if or else will be executed.<\/p>\n<p>We have printed the specified string and they are not equal. So, the string comparison method returned the else statement \u201cThe strings are not equal\u201d.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"698\" height=\"106\" class=\"wp-image-18466\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-2.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-2.jpeg 698w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-2-300x46.jpeg 300w\" sizes=\"auto, (max-width: 698px) 100vw, 698px\" \/><\/strong><\/p>\n<h2><strong>Example 2: Bash String Comparison with \u201c==\u201d Operator in Ubuntu 22.04<\/strong><\/h2>\n<p>As the bash script does not have any built-in function for string comparison like other programming languages, we used these operators for the bash string comparison. In the above example, we used the equality operator. But we can also compare the string with the double equality \u201c==\u201d operator.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"206\" class=\"wp-image-18469\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-3.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-3.jpeg 729w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-3-300x85.jpeg 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/strong><\/p>\n<p>Here, we have used the read command with the \u201c-p\u201d option for strings input by the user. The user enters the name and the degree name as the string. The strings will be stored by the attributes \u201cstring1\u201d and \u201cstring2\u201d. After that, we have an if-else statement where we have compared the strings \u201cstring1\u201d and \u201cstring2\u201d by using the \u201c==\u201d operator. If the two strings are compared, it will determine whether they are identical. The echo statements of the if-else will be returned upon string comparison.<\/p>\n<p>The user inputs the strings on the shell script. Then, these strings are compared with the equality \u201c==\u201d operator and they are not the same. Hence, the string equality results are displayed on the screen.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"93\" class=\"wp-image-18472\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-4.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-4.jpeg 650w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-4-300x43.jpeg 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/strong><\/p>\n<h2><strong>Example 3: Bash String Comparison with \u201c!=\u201d operator in Ubuntu 22.04<\/strong><\/h2>\n<p>We have checked the equality to compare the bash strings. Now, instead of checking the equality, we will check the inequality between the strings. For this, we have an operator \u201c!=\u201d which is called the \u201cnot equal to\u201d operator.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"620\" height=\"213\" class=\"wp-image-18477\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-5.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-5.jpeg 620w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-5-300x103.jpeg 300w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/strong><\/p>\n<p>We have given a string value inside the attribute \u201cMyString\u201d. Then, we have called this attribute in the if statement. The if statement holds the \u201cMyString\u201d and the other string value for inequality comparison with the \u201c!=\u201d operator. The echo statement will be displayed if the strings are not equal. The echo statement of else will execute when the strings are matched.<\/p>\n<p>The bash strings comparison displayed the inequality statement as the bash strings have different values.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"632\" height=\"55\" class=\"wp-image-18480\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-6.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-6.jpeg 632w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-6-300x26.jpeg 300w\" sizes=\"auto, (max-width: 632px) 100vw, 632px\" \/><\/strong><\/p>\n<h2><strong>Example 4: Bash String Comparison with \u201c=\u201d, \u201c&lt;\u201d, and \u201c&gt;\u201d operators in Ubuntu 22.04<\/strong><\/h2>\n<p>Now, we have used other operators like greater than and less than for comparison of the bash strings. We have utilized all these operators with the elif conditions of the bash script.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"738\" height=\"250\" class=\"wp-image-18482\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-7.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-7.jpeg 738w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-7-300x102.jpeg 300w\" sizes=\"auto, (max-width: 738px) 100vw, 738px\" \/><\/strong><\/p>\n<p>We have created two bash attributes \u201cMyStr1\u201d and \u201cMyStr2\u201d which are assigned with the string values to these attributes. Then, we constructed the elif conditions where these strings are compared. Between these strings, we have used the equal \u201c=\u201d operator, less than the \u201c&lt;\u201d operator, and the greater than \u201c&gt;\u201d operator. One statement will be executed upon a comparison of the bash string attributes.<\/p>\n<p>As the strings are not equal so the first statement is false, we have a second statement for greater than which is also false. The last statement of less than comparison of the string is printed as it returns true results.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"631\" height=\"56\" class=\"wp-image-18485\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-8.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-8.jpeg 631w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-8-300x27.jpeg 300w\" sizes=\"auto, (max-width: 631px) 100vw, 631px\" \/><\/strong><\/p>\n<h2><strong>Example 5: Bash Empty String Validation with \u201cz\u201d Operator in Ubuntu 22.04<\/strong><\/h2>\n<p>Bash allows us to determine whether a string is empty or null by using the \u201c-z\u201d operator. The \u201c-z\u201d operator determines whether the string length is zero or not. The condition is met if the string attribute has no value.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"643\" height=\"184\" class=\"wp-image-18488\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-9.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-9.jpeg 643w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-9-300x86.jpeg 300w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/strong><\/p>\n<p>We have declared the bash attribute as \u201cStrVal\u201d which has the empty \u2018\u2019 string. After this, we used the \u201c-z\u201d operator with the \u201cStrVal&#8221; attribute. The \u201c-z\u201d operator verifies the bash string is empty or has some string value.<\/p>\n<p>As the string is empty, the \u201c-z\u201d operator returns true. The if statement is printed on the bash shell.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"673\" height=\"73\" class=\"wp-image-18491\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-10.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-10.jpeg 673w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-10-300x33.jpeg 300w\" sizes=\"auto, (max-width: 673px) 100vw, 673px\" \/><\/strong><\/p>\n<h2><strong>Example 6: Bash Non-Empty String Validation with \u201cn\u201d Operator in Ubuntu 22.04<\/strong><\/h2>\n<p>The \u201c-n\u201d operator is used to validate that the bash string is not empty. The condition will allow us to determine if the provided bash string is not empty. Its functionality is demonstrated by the example below:<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"191\" class=\"wp-image-18493\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-11.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-11.jpeg 696w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-11-300x82.jpeg 300w\" sizes=\"auto, (max-width: 696px) 100vw, 696px\" \/><\/strong><\/p>\n<p>The string attribute is declared as \u201cStringIs\u201d and is initialized with the non-empty string value. Then, we have an if condition that uses the bash \u201c-n\u201d operator for validating the non-empty string. We have also specified the attribute \u201cStringIs\u201d inside the if condition with the \u201c-n\u201d operator.<\/p>\n<p>The string is not empty as verified by the \u201c-n\u201d operator so the statement of string existence is displayed below.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"595\" height=\"58\" class=\"wp-image-18498\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-12.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-12.jpeg 595w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-12-300x29.jpeg 300w\" sizes=\"auto, (max-width: 595px) 100vw, 595px\" \/><\/strong><\/p>\n<h2><strong>Example 7: Bash String Comparison for a Substring in Ubuntu 22.04<\/strong><\/h2>\n<p>We will compare the string with the substring by surrounding the substring with the asterisk \u201c*\u201d symbol which matches all the characters of the string.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"626\" height=\"166\" class=\"wp-image-18505\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-13.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-13.jpeg 626w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-13-300x80.jpeg 300w\" sizes=\"auto, (max-width: 626px) 100vw, 626px\" \/><\/strong><\/p>\n<p>We have defined the bash string to the bash attribute. Then, we compared the substring \u201cbash\u201d with the string in \u201cAttribute\u201d. If the given string contains the substring, then, the echo message will be obtained.<\/p>\n<p>The \u201cbash\u201d substring is found in the given bash string, so the substring exists message is seen on the bash terminal.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"627\" height=\"57\" class=\"wp-image-18506\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-14.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-14.jpeg 627w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18461-14-300x27.jpeg 300w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/strong><\/p>\n<h2><strong>Conclusion: <\/strong><\/h2>\n<p>String comparison in bash will be simpler after accomplishing the aforementioned examples. We have used the equality operator for the bash string comparison which takes the strings from the users in the bash terminal. Then, we utilize the inequality operator for the comparison of the strings. We have also compared the bash strings with the \u201c\/&lt;\u201d and \u201c\/&gt;\u201d operators. Furthermore, bash provides the \u201c-z\u201d and \u201c-n\u201d operators for validating the empty and non-empty strings which are explained in the bash script.<\/p>","protected":false},"excerpt":{"rendered":"<p>In this article, we will see the comparison of the strings in Bash scripting, especially with the Bash scripts if\/else method.<\/p>","protected":false},"author":105,"featured_media":18677,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1050],"tags":[],"class_list":["post-18461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-programming"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/18461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/105"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=18461"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/18461\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/18677"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=18461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=18461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=18461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}