{"id":17920,"date":"2022-08-03T18:49:08","date_gmt":"2022-08-03T18:49:08","guid":{"rendered":"https:\/\/linuxways.net\/?p=17920"},"modified":"2022-08-17T03:58:50","modified_gmt":"2022-08-17T03:58:50","slug":"compare-numbers-bash-script","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/compare-numbers-bash-script\/","title":{"rendered":"How to Compare Numbers in a Bash Script"},"content":{"rendered":"<p>Number comparison is considered as an important arithmetic operation that lets you find out the relative magnitude of one number in comparison to another. Bash also allows you to compare numbers very easily. Therefore, in this article, we will teach you how you can use a Bash script for comparing two numbers.<\/p>\n<h2><strong>Number Comparisons in Bash Scripts<\/strong><\/h2>\n<p>Six different comparison operators are used for comparing the numbers in a Bash script in Linux. All of these operators are used in the following examples:<\/p>\n<h3><strong>Example 1: Using the Equal and Not Equal Comparison Operators<\/strong><\/h3>\n<p>In this example, we will use the equal and not equal operators in Bash for comparing two numbers. We use the Bash script shown in the following image for this purpose:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"642\" height=\"337\" class=\"wp-image-17938\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-1-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-1-1.png 642w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-1-1-300x157.png 300w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/p>\n<p>In this Bash script, we first used the shebang for declaring our document as a Bash script. Then, we defined two variables, \u201cvar1\u201d and \u201cvar2\u201d, and assigned to them to two different integers. Then, we used an \u201cif\u201d statement for checking if these two numbers are equal. If yes, a relevant message is printed on the terminal. After that, we have another \u201cif\u201d statement in which we used the \u201cnot equal\u201d operator for checking if these numbers are not equal. If this statement is evaluated to true, a relevant message is printed on the terminal.<\/p>\n<p>Then, after saving our Bash file, we executed it with the following command:<\/p>\n<pre>\r\n$ bash compare.sh\r\n<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"351\" height=\"27\" class=\"wp-image-17939\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-2-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-2-1.png 351w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-2-1-300x23.png 300w\" sizes=\"auto, (max-width: 351px) 100vw, 351px\" \/><\/strong><\/p>\n<p>Upon execution, we found out that our second \u201cif\u201d statement turned out to be true since the two specified numbers were not equal as shown in the following image:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"378\" height=\"63\" class=\"wp-image-17940\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-3-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-3-1.png 378w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-3-1-300x50.png 300w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><\/p>\n<h3><strong>Example 2: Using the Greater Than and Greater Than or Equal To Operators<\/strong><\/h3>\n<p>In this example, we will use the greater than and the greater than or equal to operators in Bash for comparing the two numbers. We use the following Bash script for this purpose:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"649\" height=\"344\" class=\"wp-image-17941\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-4-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-4-1.png 649w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-4-1-300x159.png 300w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/p>\n<p>Just like the first example, we defined the two variables in this script and kept their values equal. After that, we used two \u201cif\u201d conditions. In the first condition, we used the \u201cgreater than\u201d operator of Bash while in the second condition, we used the \u201cgreater than or equal to\u201d operator of Bash for comparing the two numbers. If the first condition is true, it means that the first number is greater than the second number while if the second condition is true, it means that these two numbers are greater than or equal to each other.<\/p>\n<p>When we ran this Bash script, we figured out that these two numbers are greater than or equal to each other as shown in the following image:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"439\" height=\"59\" class=\"wp-image-17942\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-5-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-5-1.png 439w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-5-1-300x40.png 300w\" sizes=\"auto, (max-width: 439px) 100vw, 439px\" \/><\/p>\n<h3><strong>Example 3: Using the Less Than and Less Than or Equal To Operators<\/strong><\/h3>\n<p>In this example, we want to use the less than and less than or equal to operators in Bash for comparing two numbers. For that, we use of the following Bash script:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"649\" height=\"335\" class=\"wp-image-17943\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-6-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-6-1.png 649w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-6-1-300x155.png 300w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/p>\n<p>Just like our first two examples, we defined the two variables in this Bash script and assigned to them the two different values. Then, we used two \u201cif\u201d conditions. The first one makes use of the \u201cless than\u201d operator while the second one makes use of the \u201cless than or equal to\u201d operator. If the first condition is true, it means that the first variable is less than the second while if the second condition is true, it means that the given variables are less than or equal to each other. However, in this case, we have chosen both the numbers in a way that both of these conditions are evaluated to true.<\/p>\n<p>You can verify this by looking at the output of this Bash script shown in the following image:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"415\" height=\"81\" class=\"wp-image-17944\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-7-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-7-1.png 415w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17920-7-1-300x59.png 300w\" sizes=\"auto, (max-width: 415px) 100vw, 415px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>With this article, we want to share with you the methods of comparing two numbers in Bash. For doing that, we shared the working of all the six number comparison operators available in Bash with you in the form of the three examples. After going through these examples, you will easily be able to compare any given numbers within a Bash script.<\/p>","protected":false},"excerpt":{"rendered":"<p>Practical tutorial on the methods on how to use a Bash script for comparing two numbers by working on the six different comparison operators available in Bash.<\/p>","protected":false},"author":3,"featured_media":18087,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-17920","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17920","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=17920"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17920\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/18087"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=17920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=17920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=17920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}