{"id":28963,"date":"2023-11-29T11:07:32","date_gmt":"2023-11-29T11:07:32","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=28963"},"modified":"2024-01-16T09:16:41","modified_gmt":"2024-01-16T09:16:41","slug":"math-in-bash","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/","title":{"rendered":"Performing Math Operations in Bash [12+ Commands Explained]"},"content":{"rendered":"<p style=\"text-align: justify;\">Mathematical or arithmetical calculations are essential for scheduling cron jobs, calculating the difference between the timestamps, and many other important tasks. There are different commands for doing math calculations in Bash. In this article, I will try to explain the commands for performing mathematical calculations with simple examples. I will also provide some practical examples where these commands can be useful.<\/p>\n<p><span style=\"font-weight: 400;\"><div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Practice-Files_math-in-bash.zip\" class=\"su-button su-button-style-default\" style=\"color:#000000;background-color:#E8F9FF;border-color:#bac8cc;border-radius:56px\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color:#000000;padding:8px 26px;font-size:20px;line-height:30px;border-color:#effbff;border-radius:56px;text-shadow:0px 0px 0px #000000\"><i class=\"sui sui-download\" style=\"font-size:20px;color:#1AA1D6\"><\/i> Practice Files for Math in Bash<\/span><\/a><\/div><\/span><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#Arithmetic_Operators_for_Integer_Floating-Point_Numbers\" >Arithmetic Operators for Integer &amp; Floating-Point Numbers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#Commands_for_Arithmetic_Operations_in_Bash\" >Commands for Arithmetic Operations in Bash<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#1_%E2%80%9Cdeclare%E2%80%9D_Command\" >1. \u201cdeclare\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#2_Arithmetic_Expansion\" >2. Arithmetic Expansion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#3_%E2%80%9Cexpr%E2%80%9D_Command\" >3. \u201cexpr\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#4_%E2%80%9Clet%E2%80%9D_Command\" >4. \u201clet\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#5_%E2%80%9Cbc%E2%80%9D_Command\" >5. \u201cbc\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#6_%E2%80%9Cdc%E2%80%9D_Command\" >6. \u201cdc\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#7_%E2%80%9Cawk%E2%80%9D_Command\" >7. \u201cawk\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#8_%E2%80%9Cprintf%E2%80%9D_Command\" >8. \u201cprintf\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#9_%E2%80%9Cpython3%E2%80%9D_Command\" >9. \u201cpython3\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#10_%E2%80%9Cperl%E2%80%9D_Command\" >10. \u201cperl\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#11_%E2%80%9Ctest%E2%80%9D_Command\" >11. \u201ctest\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#12_Assignment_Operator\" >12. Assignment Operator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#13_Built-in_Math_Functions_in_Bash\" >13. Built-in Math Functions in Bash<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#Mathematical_Calculation_in_Different_Arithmetic_Bases\" >Mathematical Calculation in Different Arithmetic Bases<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#Practical_Examples_of_Mathematical_Operations_in_Bash\" >Practical Examples of Mathematical Operations in Bash<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#A_Finding_the_Maximum_of_Three_Numbers\" >A. Finding the Maximum of Three Numbers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#B_How_to_Find_the_Factorial_of_a_Number_in_Bash\" >B. How to Find the Factorial of a Number in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#C_Calculating_Difference_Between_Two_Dates_in_Bash\" >C. Calculating Difference Between Two Dates in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#D_Bits_to_Bytes_Conversion_in_Bash\" >D. Bits to Bytes Conversion in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#E_How_to_Find_LCM_and_GCD_of_Two_Numbers_in_Bash\" >E. How to Find LCM and GCD of Two Numbers in Bash<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#People_Also_Ask\" >People Also Ask<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#Why_%E2%80%9Cvalue_too_great_for_base%E2%80%9D_error_occurs_in_arithmetic_calculation_in_Bash\" >Why \u201cvalue too great for base\u201d error occurs in arithmetic calculation in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#How_to_solve_the_%E2%80%9Csyntax_error_invalid_arithmetic_operator%E2%80%9D_in_Bash\" >How to solve the \u201csyntax error: invalid arithmetic operator\u201d in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#How_to_solve_the_%E2%80%9Cnon-integer_argument%E2%80%9D_error_in_Bash\" >How to solve the \u201cnon-integer argument\u201d error in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-28\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#How_can_I_generate_random_numbers_for_mathematical_calculation_in_Bash\" >How can I generate random numbers for mathematical calculation in Bash?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Arithmetic_Operators_for_Integer_Floating-Point_Numbers\"><\/span>Arithmetic Operators for Integer &amp; Floating-Point Numbers<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The Bash shell is primarily designed for performing arithmetic operations with whole numbers, both positive and negative, such as 1, -5, and 10. There are many <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/\" target=\"_blank\" rel=\"noopener\"><strong>arithmetic operators<\/strong><\/a> available in Bash such as addition, multiplication, subtraction, etc. These arithmetic operators facilitate arithmetic calculation. The below list contains basic arithmetic operators that users can use in Bash:<\/p>\n<div class=\"su-table su-table-responsive su-table-alternate su-table-fixed\">\n<table style=\"width: 100%;height: 175px\" width=\"100%\">\n<tbody>\n<tr style=\"height: 25px\">\n<th style=\"height: 25px\" width=\"286\"><strong>Operators<\/strong><\/th>\n<th style=\"height: 25px\" width=\"338\"><strong>Operation<\/strong><\/th>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"286\"><strong>+<\/strong><\/td>\n<td style=\"vertical-align: middle;height: 25px;text-align: center\" width=\"338\">Addition<\/td>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"286\"><strong>&#8211;<\/strong><\/td>\n<td style=\"vertical-align: middle;height: 25px;text-align: center\" width=\"338\">Subtraction<\/td>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"286\"><strong>*<\/strong><\/td>\n<td style=\"vertical-align: middle;height: 25px;text-align: center\" width=\"338\">Multiplication<\/td>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"286\"><strong>\/<\/strong><\/td>\n<td style=\"vertical-align: middle;height: 25px;text-align: center\" width=\"338\">Division<\/td>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"286\"><strong>%<\/strong><\/td>\n<td style=\"vertical-align: middle;height: 25px;text-align: center\" width=\"338\">Modulus (remainder)<\/td>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"286\"><strong>**<\/strong><\/td>\n<td style=\"vertical-align: middle;height: 25px;text-align: center\" width=\"338\">Exponentiation<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"text-align: justify;\">These operators can be used to perform arithmetic operations directly on integers, as well as on variables that contain integer values. However, when it comes to dealing with floating-point numbers like 3.14 or 2.5, users often need to rely on external utilities.<\/p>\n<p style=\"text-align: justify;\">From this perspective, commands and utilities for arithmetic operations in Bash can be grouped into two categories- those that support floating point arithmetic and those that can\u2019t.<\/p>\n<div class=\"su-table su-table-responsive su-table-alternate su-table-fixed\">\n<table style=\"width: 100%;height: 175px\" width=\"100%\">\n<tbody>\n<tr style=\"height: 25px\">\n<th style=\"height: 25px\" width=\"302\"><strong>Does Not Support Floating-Point <\/strong><\/th>\n<th style=\"height: 25px\" width=\"322\"><strong>Supports Floating-Point Arithmetic<\/strong><\/th>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"302\">declare -i<\/td>\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"322\">bc<\/td>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"302\">Arithmetic expansion<\/td>\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"322\">dc<\/td>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"302\">expr<\/td>\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"322\">awk<\/td>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"302\">let<\/td>\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"322\">printf<\/td>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"302\">test<\/td>\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"322\">python2 or python3<\/td>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"302\">Assignment Operator<\/td>\n<td style=\"text-align: center;vertical-align: middle;height: 25px\" width=\"322\">perl<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"text-align: justify;\">It is obvious that the functions that support floating point can also perform integer arithmetic.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Commands_for_Arithmetic_Operations_in_Bash\"><\/span>Commands for Arithmetic Operations in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Now, I am going to discuss various commands and utilities for performing arithmetic operations. First, I will cover built-in commands such as <a href=\"https:\/\/linuxsimply.com\/declare-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>declare<\/strong><\/a> and <strong>let<\/strong>. Then, I will delve into external utilities like <a href=\"https:\/\/linuxsimply.com\/awk-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>awk<\/strong><\/a>, <a href=\"https:\/\/linuxsimply.com\/bc-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>bc<\/strong><\/a>, <strong>perl<\/strong>, etc. Let\u2019s continue.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_%E2%80%9Cdeclare%E2%80%9D_Command\"><\/span>1. \u201cdeclare\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>declare<\/strong> command is used to set values to variables. The command option <strong>-i <\/strong>\u00a0is for declaring variables with integer values. One can perform mathematical calculations by defining a proper arithmetic expression by the following syntax:<\/p>\n<p><code class=\"\" data-line=\"\">declare -i var=expression<\/code><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">declare -i A=(2+3)**2\necho $A<\/code><\/pre>\n<p style=\"text-align: justify;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-28967\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/1.-declare-command-for-arithmetic-calculation-in-Bash.png\" alt=\"declare command for arithmetic calculation in Bash\" width=\"617\" height=\"144\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/1.-declare-command-for-arithmetic-calculation-in-Bash.png 617w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/1.-declare-command-for-arithmetic-calculation-in-Bash-300x70.png 300w\" sizes=\"(max-width: 617px) 100vw, 617px\" \/>Here, the expression <code class=\"\" data-line=\"\">(2+3)**2<\/code> is evaluated first, resulting in 25. This calculated result later becomes the value of the variable <code class=\"\" data-line=\"\">A<\/code>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Arithmetic_Expansion\"><\/span>2. Arithmetic Expansion<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/arithmetic-expansion\/\" target=\"_blank\" rel=\"noopener\"><strong>Arithmetic expansion<\/strong><\/a> is the built-in way to perform mathematical calculations in Bash. One can calculate a valid expression by placing it within the double parentheses of <strong>$((&#8230;))<\/strong>. For instance,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo $((10*2\/5))<\/code><\/pre>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-28968\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/2.-Arithmetic-expansion-for-mathematical-calculation-in-Bash.png\" alt=\"Arithmetic expansion for mathematical calculation in Bash\" width=\"522\" height=\"113\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/2.-Arithmetic-expansion-for-mathematical-calculation-in-Bash.png 522w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/2.-Arithmetic-expansion-for-mathematical-calculation-in-Bash-300x65.png 300w\" sizes=\"(max-width: 522px) 100vw, 522px\" \/>Here, arithmetic expansion calculates the result of multiplying <strong>10<\/strong> by<strong> 2<\/strong> and then dividing the product by <strong>5<\/strong>. The result of the calculation is <strong>4<\/strong> as you can see in the image.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_%E2%80%9Cexpr%E2%80%9D_Command\"><\/span>3. \u201cexpr\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>expr<\/strong> command serves as an alternative to arithmetic expansion in Bash. It allows evaluating an expression when written appropriately following the word expr itself. The basic syntax is as follows:<\/p>\n<p><code class=\"\" data-line=\"\">expr expression<\/code><\/p>\n<p>Let\u2019s perform some calculations using the <strong>expr<\/strong> command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">expr 2 + 3\nexpr 2 \\* 3<\/code><\/pre>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-28969\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/3.-expr-command-for-math-in-Bash.png\" alt=\"expr command for math in Bash\" width=\"436\" height=\"132\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/3.-expr-command-for-math-in-Bash.png 436w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/3.-expr-command-for-math-in-Bash-300x91.png 300w\" sizes=\"(max-width: 436px) 100vw, 436px\" \/><code class=\"\" data-line=\"\">2 + 3<\/code> performs the addition of 2 and 3.<\/p>\n<p style=\"text-align: justify;\"><code class=\"\" data-line=\"\">2 \\* 3<\/code> performs the multiplication of 2 and 3. Here, the multiplication sign <code class=\"\" data-line=\"\">*<\/code> is placed after the escaped character backslash <code class=\"\" data-line=\"\">\\<\/code> because the asterisk has a different meaning to Bash shell.<\/p>\n<p style=\"text-align: justify;\">The space before and after the operator used in the <strong>expr<\/strong> command is important. If you remove those spaces, it returns back the expression itself. See the following image,<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28970\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/4.-Proper-spacing-in-expr-command-for-math-in-Bash.png\" alt=\"Proper spacing in expr command for math in Bash\" width=\"393\" height=\"112\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/4.-Proper-spacing-in-expr-command-for-math-in-Bash.png 393w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/4.-Proper-spacing-in-expr-command-for-math-in-Bash-300x85.png 300w\" sizes=\"(max-width: 393px) 100vw, 393px\" \/> Moreover, the <strong>expr<\/strong> command can\u2019t perform exponential operations. For example, the following command won\u2019t work:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">expr 2 \\*\\* 3<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28971\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/5.-Error-in-expr-command-while-performing-exponential-in-Bash.png\" alt=\"Error in expr command while performing exponential in Bash\" width=\"700\" height=\"118\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/5.-Error-in-expr-command-while-performing-exponential-in-Bash.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/5.-Error-in-expr-command-while-performing-exponential-in-Bash-300x51.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_%E2%80%9Clet%E2%80%9D_Command\"><\/span>4. \u201clet\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>let<\/strong> command allows users to perform mathematical calculations in a concise manner. The basic syntax is given below. Just replace the <strong>expression <\/strong>with the expression you want to evaluate.<\/p>\n<p><code class=\"\" data-line=\"\">let &quot;var=expression&quot;<\/code><\/p>\n<p>See the following example of adding 2 and 3 using the <strong>let <\/strong>command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">let &quot;result=2+3&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28972\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/6.-let-command-for-math-in-Bash.png\" alt=\"let command for math in Bash\" width=\"548\" height=\"100\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/6.-let-command-for-math-in-Bash.png 548w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/6.-let-command-for-math-in-Bash-300x55.png 300w\" sizes=\"(max-width: 548px) 100vw, 548px\" \/>The result of addition <strong>5 <\/strong>is stored in the <strong>result<\/strong> variable. Then the <a href=\"https:\/\/linuxsimply.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>echo command<\/strong><\/a> displayed the <strong>result <\/strong>as you can see in the image above.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"5_%E2%80%9Cbc%E2%80%9D_Command\"><\/span>5. \u201cbc\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The command<strong> bc<\/strong> stands for <strong>Basic Calculator<\/strong>. It is an easy-to-use command that allows users to perform arbitrary-precision arithmetic calculations. So it supports floating point arithmetic. Thus, the limitation of <strong>arithmetic expansion<\/strong> and the <strong>expr<\/strong> command can be overcome through this command tool.<\/p>\n<p style=\"text-align: justify;\">To evaluate an expression using this command, you must first write the expression correctly and then pipe it to the command.<\/p>\n<p><code class=\"\" data-line=\"\">&quot;expression&quot; | bc<\/code><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo &quot;10 * (3 + 2)&quot; | bc<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28973\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/7.-bc-command-for-math.png\" alt=\"bc command for math\" width=\"660\" height=\"118\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/7.-bc-command-for-math.png 660w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/7.-bc-command-for-math-300x54.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/>In the above image, I want to evaluate the expression <strong>&#8220;10 * (3 + 2)&#8221;<\/strong>.\u00a0 It represents the multiplication of 10 by the sum of 3 and 2. The <strong>echo<\/strong> command is used to write the expression and <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/piping\/\" target=\"_blank\" rel=\"noopener\"><strong>pipe (|)<\/strong><\/a> is used to redirect the standard output to the<strong> bc <\/strong>command. The <strong>bc<\/strong> command evaluates the expression and shows the result <strong>50<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"6_%E2%80%9Cdc%E2%80%9D_Command\"><\/span>6. \u201cdc\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>dc<\/strong> command stands for <strong>desk calculator<\/strong>. It is a Reverse Polish Notation(RPN) calculator. That means the numbers of a calculation are given first then the operator is defined. The basic syntax is similar to the <strong>bc<\/strong> command. Such as,<\/p>\n<p><code class=\"\" data-line=\"\">&quot;expression&quot; | dc<\/code><\/p>\n<p>Look at the following command that evaluates the expression <strong>&#8220;10 * (3 + 2)&#8221;<\/strong>:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo &quot;3 2 + 10 * p&quot; | dc<\/code><\/pre>\n<p style=\"text-align: justify;\">Here, the relationship between 3 and 2 is addition. The addition sign is placed after the numbers are given. After the addition operator adds the top two elements of the stack (3 and 2), 10 is pushed onto the stack.<\/p>\n<p style=\"text-align: justify;\">Then the multiplication operator <code class=\"\" data-line=\"\">*<\/code> multiplies <strong>5<\/strong> and <strong>10<\/strong> and pushes the result <strong>(50)<\/strong> back onto the stack.<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28974\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/8.-dc-command-for-math-in-Bash.png\" alt=\"dc command for math in Bash\" width=\"651\" height=\"125\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/8.-dc-command-for-math-in-Bash.png 651w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/8.-dc-command-for-math-in-Bash-300x58.png 300w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"7_%E2%80%9Cawk%E2%80%9D_Command\"><\/span>7. \u201cawk\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>awk <\/strong>command is a text-processing utility that also supports mathematical calculations. It can handle floating point arithmetic. One can write the expression to be evaluated within the <strong>BEGIN<\/strong> block or use the <strong>printf <\/strong>command. Let\u2019s evaluate the expression <strong>10 * (3 + 2) <\/strong>using the awk:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo &quot;10 3 2&quot; | awk &#039;{printf &quot;%.0f\\n&quot;, $1 ^ ($2 + $3)}&#039;<\/code><\/pre>\n<p style=\"text-align: justify;\">Here,the <strong>echo <\/strong>command is used to pass the numbers involved in the calculation. Then these numbers are piped to the <strong>awk<\/strong> command. The <strong>awk<\/strong> command reads the numbers using the <code class=\"\" data-line=\"\">$1<\/code>, <code class=\"\" data-line=\"\">$2<\/code>, and <code class=\"\" data-line=\"\">$3<\/code> respectively. Within the printf block the <strong>10 * (3 + 2) <\/strong>is defined using the<code class=\"\" data-line=\"\"> $1 ^ ($2 + $3)<\/code>. Format specifier <code class=\"\" data-line=\"\">&quot;%.0f\\n&quot;<\/code> indicates that no decimal places should be shown, effectively rounding the result to an integer.<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28975\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/9.-awk-command-for-math-in-Bash.png\" alt=\"awk command for math in Bash\" width=\"825\" height=\"75\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/9.-awk-command-for-math-in-Bash.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/9.-awk-command-for-math-in-Bash-300x27.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/9.-awk-command-for-math-in-Bash-768x70.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/> The command outputs the result by rounding to an integer, that is <strong>50<\/strong>.<\/p>\n<p>Now, look at the use of the <strong>BEGIN<\/strong> block to evaluate the addition of 3 and 2:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">awk &#039;BEGIN {print 3 + 2}&#039;<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28976\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/10.-awk-command-with-BEGIN-block-for-performing-arithmetic-calculation.png\" alt=\"awk command with BEGIN block for performing arithmetic calculation\" width=\"656\" height=\"112\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/10.-awk-command-with-BEGIN-block-for-performing-arithmetic-calculation.png 656w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/10.-awk-command-with-BEGIN-block-for-performing-arithmetic-calculation-300x51.png 300w\" sizes=\"(max-width: 656px) 100vw, 656px\" \/>The output of the expression <strong>\u20185\u2019<\/strong> is displayed.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"8_%E2%80%9Cprintf%E2%80%9D_Command\"><\/span>8. \u201cprintf\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>printf<\/strong> command with arithmetic expansion can perform arithmetic calculations up to the desired precision. The general syntax is as follows:<\/p>\n<p><code class=\"\" data-line=\"\">printf &quot;%.&lt;precision&gt;f&quot; $((10**&lt;precision&gt;*expression))e-&lt;precision&gt;<\/code><\/p>\n<p style=\"text-align: justify;\">To perform a certain calculation replace the <code class=\"\" data-line=\"\">expression<\/code> with the expression you want to evaluate and set your desired precision value in place of <code class=\"\" data-line=\"\">precision<\/code> in the above syntax. Look at the following example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo printf &quot;%.4f&quot; $((10**4*(10*4\/20)))e-4<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28977\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/11.-printf-command-to-perform-arbitrary-precision-arithmetic.png\" alt=\"printf command to perform arbitrary precision arithmetic\" width=\"825\" height=\"87\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/11.-printf-command-to-perform-arbitrary-precision-arithmetic.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/11.-printf-command-to-perform-arbitrary-precision-arithmetic-300x32.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/11.-printf-command-to-perform-arbitrary-precision-arithmetic-768x81.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> OUTPUT ANALYSIS<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">Here, <code class=\"\" data-line=\"\">$((10**4*(10*4\/20)))<\/code> performs arithmetic expansion. First, 10 raised to the power of 4, which is 10000. Then 10 is multiplied by 4, yielding 40. Finally, 40 is divided by 2, resulting in 2. 10000 multiplied by 2 gives 20000.<\/p>\n<p style=\"text-align: justify;\">In <code class=\"\" data-line=\"\">$((10**2*5\/2))e-4<\/code>, the result of arithmetic expansion is multiplied by <code class=\"\" data-line=\"\">e-4<\/code> which represents multiplying the preceding value by 10 to the power of -4. In this case, it means dividing the preceding value by 10000. So, <code class=\"\" data-line=\"\">20000e-4<\/code> is equivalent to 2.0000.<\/p>\n<p style=\"text-align: justify;\">The printf <code class=\"\" data-line=\"\">&quot;%.4f\\n&quot;<\/code> statement specifies a format for printing. <code class=\"\" data-line=\"\">%.4f<\/code> specifies that the number should be formatted as a floating point with four decimal places.<\/div><\/div>\n<h3><span class=\"ez-toc-section\" id=\"9_%E2%80%9Cpython3%E2%80%9D_Command\"><\/span>9. \u201cpython3\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">One can use <a href=\"https:\/\/www.python.org\/\" target=\"_blank\" rel=\"noopener\"><strong>Python<\/strong><\/a> functionality in Bash using the <strong>python2<\/strong> and <strong>python3 <\/strong>commands. In the following command, I use <strong>python3 <\/strong>to perform the simple multiplication of <strong>5<\/strong> and <strong>3<\/strong>:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">python3 -c &quot;a = 5; b = 3; print(a * b)&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28978\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/12.-python-command-for-math-in-Bash.png\" alt=\"python command for math in Bash\" width=\"825\" height=\"106\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/12.-python-command-for-math-in-Bash.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/12.-python-command-for-math-in-Bash-300x39.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/12.-python-command-for-math-in-Bash-768x99.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>One can even import Python libraries and use their built-in functions for math calculations. <a href=\"https:\/\/docs.python.org\/3\/library\/math.html\" target=\"_blank\" rel=\"noopener\"><strong>math<\/strong><\/a> is one of the popular libraries in Python for mathematical tasks. Let\u2019s import <strong>math <\/strong>and use its <strong>sqrt <\/strong>function to calculate the square root of a number:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">python -c &quot;import math; print(math.sqrt(25))&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28979\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/13.-Python-math-library-for-arithmetic-calculation-in-Bash.png\" alt=\"Python math library for arithmetic calculation in Bash\" width=\"825\" height=\"90\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/13.-Python-math-library-for-arithmetic-calculation-in-Bash.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/13.-Python-math-library-for-arithmetic-calculation-in-Bash-300x33.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/13.-Python-math-library-for-arithmetic-calculation-in-Bash-768x84.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>In the above example, I imported the math library to calculate the square root of <strong>25<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"10_%E2%80%9Cperl%E2%80%9D_Command\"><\/span>10. \u201cperl\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Like using Python, one can execute <a href=\"https:\/\/www.perl.org\/\" target=\"_blank\" rel=\"noopener\"><strong>perl<\/strong><\/a> code from the terminal. The <strong>perl<\/strong> command with the <strong>-e <\/strong>option can perform arithmetic calculations. Along with basic math operations, one can use built-in math functions too. For example,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">perl -e &#039;print sqrt(25) + 3**2, &quot;\\n&quot;;&#039;<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28980\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/14.-perl-code-in-Bash-for-arithmetic-calculation.png\" alt=\"perl code in Bash for arithmetic calculation\" width=\"825\" height=\"73\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/14.-perl-code-in-Bash-for-arithmetic-calculation.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/14.-perl-code-in-Bash-for-arithmetic-calculation-300x27.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/14.-perl-code-in-Bash-for-arithmetic-calculation-768x68.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>In the above example, I used the print statement to output the result in the terminal. In the calculation part, <code class=\"\" data-line=\"\">sqrt(25)<\/code> calculated the square root of 25, which is 5. Then added it with 3 raised to the power of 2, resulting in 14.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"11_%E2%80%9Ctest%E2%80%9D_Command\"><\/span>11. \u201ctest\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>test<\/strong> command in Bash can evaluate conditional expressions and result in either 0 or 1 for true or false conditions respectively. For instance,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">test 5 -gt 2; echo $?<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28981\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/15.-test-command-for-evaluating-arithmetic-condition-in-Bash.png\" alt=\"test command for evaluating arithmetic condition in Bash\" width=\"608\" height=\"116\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/15.-test-command-for-evaluating-arithmetic-condition-in-Bash.png 608w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/15.-test-command-for-evaluating-arithmetic-condition-in-Bash-300x57.png 300w\" sizes=\"(max-width: 608px) 100vw, 608px\" \/>Here, <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/dollar-sign\/\" target=\"_blank\" rel=\"noopener\"><strong>$?<\/strong><\/a> checks the <strong>exit status<\/strong> of the previous command. The exit status of the previous command is 0 as 5 greater than 2 is a true condition.<\/p>\n<p style=\"text-align: justify;\">Alternatively, one can use square brackets instead of the test command to evaluate a mathematical condition. For that use the following command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">[ 5 -gt 10 ]; echo $?<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28982\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/16.-Double-square-bracket-for-evaluating-math-condition-in-Bash.png\" alt=\"Double square bracket for evaluating math condition in Bash\" width=\"596\" height=\"110\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/16.-Double-square-bracket-for-evaluating-math-condition-in-Bash.png 596w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/16.-Double-square-bracket-for-evaluating-math-condition-in-Bash-300x55.png 300w\" sizes=\"(max-width: 596px) 100vw, 596px\" \/>Here the exit status is 1 because 5 greater than 10 is a false statement.<\/p>\n<div class=\"su-note\"  style=\"border-color:#cedfe2;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#e8f9fc;border-color:#ffffff;color:#333333;\">\n<p style=\"text-align: justify;\"><strong>Note:<\/strong> Make sure you put a space after and before the starting and the closing square brackets respectively. Otherwise, there will be a syntax error and exit status will be different and not the proper representation of the evaluation of the condition. <\/div><\/div>\n<h3><span class=\"ez-toc-section\" id=\"12_Assignment_Operator\"><\/span>12. Assignment Operator<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Assignment operators are used to assign values to variables. One can use the <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/#Assignment_Operators_for_Arithmetic_Calculation_in_Bash\" target=\"_blank\" rel=\"noopener\"><strong>assignment operators<\/strong><\/a> to perform basic arithmetic such as addition, multiplication, division, etc. Let\u2019s perform the addition operation using the \u201c+=\u201d assignment operator:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">x=5\n(( x += 15 ))\necho &quot;After Increment, x=$x&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28983\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/17.-Increment-operator-for-addition-in-Bash.png\" alt=\"Increment operator for addition in Bash\" width=\"700\" height=\"175\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/17.-Increment-operator-for-addition-in-Bash.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/17.-Increment-operator-for-addition-in-Bash-300x75.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The initial value of x is <strong>5<\/strong>. After that, x is increased by <strong>15<\/strong>. So the final value of x is <strong>20<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"13_Built-in_Math_Functions_in_Bash\"><\/span>13. Built-in Math Functions in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Apart from the commands for basic mathematical calculation, there are few built-in commands for specific math tasks- like the <strong>factor<\/strong> command. This command is used for the prime factorization of an integer number. It can even factor multiple numbers at a time. See the below example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">factor 30\nfactor 30 60<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28984\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/18.-factor-command-for-prime-factorization-of-numbers-in-Bash.png\" alt=\"factor command for prime factorization of numbers in Bash\" width=\"477\" height=\"218\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/18.-factor-command-for-prime-factorization-of-numbers-in-Bash.png 477w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/18.-factor-command-for-prime-factorization-of-numbers-in-Bash-300x137.png 300w\" sizes=\"(max-width: 477px) 100vw, 477px\" \/>The first command gives the prime factors of <strong>30<\/strong>. The second command gives the factors of 30 and 60 at a time. You can even provide more than two arguments in the factor command and get the prime factorization of all the given numbers.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Mathematical_Calculation_in_Different_Arithmetic_Bases\"><\/span>Mathematical Calculation in Different Arithmetic Bases<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Bash arithmetic is not limited to decimal-based numbers. One can easily change the base and perform necessary arithmetic calculations. To define the base of a number use the following syntax:<\/p>\n<p><code class=\"\" data-line=\"\">base#number<\/code><\/p>\n<p style=\"text-align: justify;\">The default value of<strong> base<\/strong> is <strong>10<\/strong> which indicates <strong>decimal number format<\/strong>. The <strong>base<\/strong> can be any integer from 2 to 64. Where <strong>2<\/strong> denotes<strong> binary number format<\/strong>. Now, let\u2019s calculate 2 plus 3 in binary number format:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo $((2#10 + 2#11))<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28985\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/19.-Binary-base-of-numbers-in-Bash.png\" alt=\"Binary base of numbers in Bash\" width=\"561\" height=\"110\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/19.-Binary-base-of-numbers-in-Bash.png 561w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/19.-Binary-base-of-numbers-in-Bash-300x59.png 300w\" sizes=\"(max-width: 561px) 100vw, 561px\" \/>You can see that the command adds <strong>10 <\/strong>and <strong>11 <\/strong>which are binary representations of <strong>2<\/strong> and <strong>3<\/strong>. The result of addition is shown in decimal number format.<\/p>\n<p style=\"text-align: justify;\">In the same manner, <strong>octal (base 8) numbers<\/strong> take the prefix <strong>0<\/strong> instead of <code class=\"\" data-line=\"\">8#<\/code> to define the base. Let\u2019s try to sum up <strong>8<\/strong> and <strong>16 <\/strong>in octal:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo $((010+020))<\/code><\/pre>\n<p style=\"text-align: justify;\">One can even change the base of the result. Look at the commands below to display the result of the addition in octal format:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">result=$((010+020))\nprintf &quot;In Octal Format: %o\\n&quot; &quot;$result&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28986\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/20.-Octal-base-for-mathematical-calculation-in-Bash.png\" alt=\"Octal base for mathematical calculation in Bash\" width=\"825\" height=\"207\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/20.-Octal-base-for-mathematical-calculation-in-Bash.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/20.-Octal-base-for-mathematical-calculation-in-Bash-300x75.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/20.-Octal-base-for-mathematical-calculation-in-Bash-768x193.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The image shows that the summation of <strong>10<\/strong> and <strong>20 <\/strong>in octal is 24 in decimal and 30 in octal.<\/p>\n<p style=\"text-align: justify;\">Finally, the <strong>hexadecimal numbers (base 16)<\/strong> can be defined using the prefix <code class=\"\" data-line=\"\">0x<\/code>. For instance, 11 in hexadecimal is <code class=\"\" data-line=\"\">0xB<\/code>. Now, I am going to sum up 11 and 2 in hex:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo $((0x2+0xB))\nresult=$((0x2+0xB))\nprintf &quot;Result in hexadecimal base: %X\\n&quot; &quot;$result&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28987\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/21.-Hexadecimal-base-for-arithmetic-calculation-in-Bash.png\" alt=\"Hexadecimal base for arithmetic calculation in Bash\" width=\"825\" height=\"194\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/21.-Hexadecimal-base-for-arithmetic-calculation-in-Bash.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/21.-Hexadecimal-base-for-arithmetic-calculation-in-Bash-300x71.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/21.-Hexadecimal-base-for-arithmetic-calculation-in-Bash-768x181.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>Here, the format specifier <code class=\"\" data-line=\"\">%X\\n<\/code> in <strong>printf <\/strong>command defines that the result should be printed in hexadecimal format. So the addition of 11 and 2 is 13 in decimal and <strong>D <\/strong>in hex.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Practical_Examples_of_Mathematical_Operations_in_Bash\"><\/span>Practical Examples of Mathematical Operations in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The best way to perform a mathematical operation in Bash depends on the nature of the calculation. Here are some practical bash scripts that heavily depend on mathematical calculation:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"A_Finding_the_Maximum_of_Three_Numbers\"><\/span>A. Finding the Maximum of Three Numbers<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To find the maximum of three numbers one needs to evaluate multiple conditional statements. Let\u2019s create a Bash script that can find the highest number among three numbers using the elif conditional statements:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Function to find the maximum of three numbers\nfind_max() {\nif [ &quot;$1&quot; -ge &quot;$2&quot; ] &amp;&amp; [ &quot;$1&quot; -ge &quot;$3&quot; ]; then\necho &quot;$1&quot;\nelif [ &quot;$2&quot; -ge &quot;$1&quot; ] &amp;&amp; [ &quot;$2&quot; -ge &quot;$3&quot; ]; then\necho &quot;$2&quot;\nelse\necho &quot;$3&quot;\nfi\n}\n\n# Call the function and store the result in the variable max\nmax=$(find_max $1 $2 $3)\n# Print the result\necho &quot;The maximum number is: $max&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The script defines a function called <strong>find_max <\/strong>that takes three positional parameters. <code class=\"\" data-line=\"\">[ &quot;$1&quot; -ge &quot;$2&quot; ] &amp;&amp; [ &quot;$1&quot; -ge &quot;$3&quot; ]<\/code> checks if the value of <code class=\"\" data-line=\"\">$1<\/code> is greater than both <code class=\"\" data-line=\"\">$2<\/code> and <code class=\"\" data-line=\"\">$3<\/code>. In the <strong>elif block,<\/strong> it compares the second positional argument with the other two. If the <strong>if block<\/strong> and the <strong>elif block <\/strong>don&#8217;t hold true by default the third positional parameter is the maximum number among the three numbers.<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28988\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/22.-Finding-maximum-of-three-numbers-in-Bash.png\" alt=\"Finding maximum of three numbers in Bash\" width=\"618\" height=\"114\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/22.-Finding-maximum-of-three-numbers-in-Bash.png 618w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/22.-Finding-maximum-of-three-numbers-in-Bash-300x55.png 300w\" sizes=\"(max-width: 618px) 100vw, 618px\" \/>Here, the three given numbers are <strong>10<\/strong>, <strong>14<\/strong> and <strong>12<\/strong>. Among the given numbers <strong>14<\/strong> is the highest as you can see in the image.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"B_How_to_Find_the_Factorial_of_a_Number_in_Bash\"><\/span>B. How to Find the Factorial of a Number in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The factorial of a non-negative integer number is denoted by the product of all positive integers less than and equal to that integer number. One can easily calculate the factorial of a number in Bash by writing a few lines of code. For example,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nfactorial () {\nif (($1 &gt; 1))\nthen\necho $(( $( factorial $(($1 - 1)) ) * $1 ))\nelse\necho 1\nreturn\nfi\n}\n\n# Call the function and store the result\nfac=$(factorial $1)\n# Print the result\necho &quot;The factorial of $1 is: $fac&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The script defines a function called <strong>factorial<\/strong>. Within this function, an <strong>if block <\/strong>checks whether the first positional parameter \u00a0(input argument) is greater than one. If this condition is true, the function recursively calls itself with the argument decremented by 1 and multiplies the result by the argument. This process continues until the argument becomes 1, effectively calculating the factorial by multiplying the original argument down to 1.<\/p>\n<p style=\"text-align: justify;\">On the other hand, if the first positional argument is already 1, the function echoes 1 as the factorial.<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28989\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/23.-Factorial-of-a-number-in-Bash.png\" alt=\"Factorial of a number in Bash\" width=\"537\" height=\"113\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/23.-Factorial-of-a-number-in-Bash.png 537w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/23.-Factorial-of-a-number-in-Bash-300x63.png 300w\" sizes=\"(max-width: 537px) 100vw, 537px\" \/>After I run the program with the argument <strong>5<\/strong>, it returns <strong>120<\/strong> as the factorial of <strong>5<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"C_Calculating_Difference_Between_Two_Dates_in_Bash\"><\/span>C. Calculating Difference Between Two Dates in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Users often try to calculate the difference between two dates. It involves converting the dates into Unix epochs and then performing the subtraction to find the difference. Let me show the process in a script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Function to calculate the difference in days between two dates\ndiff_in_days() {\nlocal start_date=&quot;$1&quot;\nlocal end_date=&quot;$2&quot;\n\n# Convert dates to Unix timestamps\nlocal start_timestamp=$(date -d &quot;$start_date&quot; +%s)\nlocal end_timestamp=$(date -d &quot;$end_date&quot; +%s)\n\n# Calculate the difference in seconds and convert to days\nlocal diff_in_seconds=$((end_timestamp - start_timestamp))\nlocal diff_in_days=$((diff_in_seconds \/ 86400))\necho &quot;$diff_in_days&quot;\n}\n\n# Prompt the user for the start date\nread -p &quot;Enter the start date (YYYY-MM-DD): &quot; start_date\n# Prompt the user for the end date\nread -p &quot;Enter the end date (YYYY-MM-DD): &quot; end_date\n\n# Calculate and print the difference in days\nresult=$(diff_in_days &quot;$start_date&quot; &quot;$end_date&quot;)\necho &quot;The difference in days between $start_date and $end_date is: $result days&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The script calculates the difference in days between two dates provided by the user. It defines a function called <strong>diff_in_days <\/strong>that takes two dates as arguments. Then it converts the dates to Unix timestamps using the <strong>date<\/strong> command with the <code class=\"\" data-line=\"\">%s<\/code> format specifier. After conversion, it calculates the difference between dates in seconds and then converts this difference to days.<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28990\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/24.-Finding-difference-between-two-dates-in-Bash.png\" alt=\"Finding difference between two dates in Bash\" width=\"825\" height=\"125\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/24.-Finding-difference-between-two-dates-in-Bash.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/24.-Finding-difference-between-two-dates-in-Bash-300x45.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/24.-Finding-difference-between-two-dates-in-Bash-768x116.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>I run the program and try to find the difference in days between the dates &#8220;2022-05-20&#8221; and &#8220;2023-07-25&#8221;. It shows that the difference between these two dates is <strong>431<\/strong> days.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"D_Bits_to_Bytes_Conversion_in_Bash\"><\/span>D. Bits to Bytes Conversion in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">One can convert bits to bytes by dividing the number of bits by 8, as there are 8 bits in a byte. Here is how you can do this in Bash:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Function to convert bits to bytes\nbits_to_bytes() {\nbits=$1\nbytes=$(echo &quot;scale=2; $bits \/ 8&quot; | bc)\necho &quot;$bits bits is $bytes bytes&quot;\n}\n\n# Call the function\nbits_to_bytes $1<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The Bash script defines a function named <strong>bits_to_bytes<\/strong> that takes a number of bits as an argument. It uses the <strong>bc<\/strong> command to perform the conversion from bits to bytes by dividing the number of bits by <strong>8<\/strong>. The <code class=\"\" data-line=\"\">scale=2<\/code> specifies that the result should have two decimal places. The script then echoes the original number of bits along with the calculated equivalent in bytes.<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28991\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/25.-Bit-to-Byte-conversion-in-Bash.png\" alt=\"Bit to Byte conversion in Bash\" width=\"578\" height=\"124\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/25.-Bit-to-Byte-conversion-in-Bash.png 578w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/25.-Bit-to-Byte-conversion-in-Bash-300x64.png 300w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/>Here, I\u2019m trying to figure out the bytes equivalent to <strong>1024<\/strong> bits. The program shows that <strong>128.00 bytes is equal to 1024 bits<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"E_How_to_Find_LCM_and_GCD_of_Two_Numbers_in_Bash\"><\/span>E. How to Find LCM and GCD of Two Numbers in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To find the Least Common Multiple (LCM) and Greatest Common Divisor (GCD) of two numbers in Bash, use a combination of loops and arithmetic expansion. For instance,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Function to calculate the Greatest Common Divisor (GCD) using Euclidean algorithm\ngcd() {\nlocal a=$1\nlocal b=$2\nwhile [ &quot;$b&quot; -ne 0 ]; do\nlocal temp=&quot;$b&quot;\nb=$((a % b))\na=&quot;$temp&quot;\ndone\necho &quot;$a&quot;\n}\n\n# Function to calculate the Least Common Multiple (LCM)\nlcm() {\nlocal a=$1\nlocal b=$2\nlocal gcd_result=$(gcd &quot;$a&quot; &quot;$b&quot;)\nlocal product=$((a * b))\necho &quot;$((product \/ gcd_result))&quot;\n}\n\n# Calculate GCD\nresult_gcd=$(gcd &quot;$1&quot; &quot;$2&quot;)\n# Calculate LCM\nresult_lcm=$(lcm &quot;$1&quot; &quot;$2&quot;)\n\n# Print the results\necho &quot;The GCD of $1 and $2 is: $result_gcd&quot;\necho &quot;The LCM of $1 and $2 is: $result_lcm&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">This script defines two functions <strong>gcd<\/strong> and <strong>lcm<\/strong>. The <strong>gcd<\/strong> function can find the Greatest Common Divisor of two numbers. The function takes two parameters, <strong>a<\/strong> and <strong>b<\/strong>, representing the numbers for which the <strong>GCD<\/strong> needs to be determined. Within the function, a <strong>while loop<\/strong> continues as long as <strong>b<\/strong> is not equal to <strong>0<\/strong>. In each iteration, it swaps the values of <strong>a<\/strong> and <strong>b<\/strong>, and updates <strong>b<\/strong> to the remainder of the division of the original <strong>a<\/strong> by the original <strong>b<\/strong>. This process continues until <strong>b<\/strong> becomes <strong>0<\/strong>, at which point the <strong>GCD<\/strong> is found.<\/p>\n<p style=\"text-align: justify;\">The <strong>lcm <\/strong>function calculates the Least Common Multiple (<strong>LCM<\/strong>) of two numbers by dividing the product of the two numbers by their <strong>GCD<\/strong>. The <strong>GCD<\/strong> is obtained by calling the <strong>gcd <\/strong>function with the same parameters.<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28992\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/26.-LCM-and-GCD-of-two-numbers-in-Bash.png\" alt=\"LCM and GCD of two numbers in Bash\" width=\"590\" height=\"146\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/26.-LCM-and-GCD-of-two-numbers-in-Bash.png 590w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/26.-LCM-and-GCD-of-two-numbers-in-Bash-300x74.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/>Here I provided two numbers <strong>12<\/strong> and <strong>30 <\/strong>to find their <strong>LCM<\/strong> and <strong>GCD<\/strong>. The program successfully showed that the GCD of the numbers is <strong>6<\/strong> and the LCM of the numbers is <strong>60<\/strong>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In conclusion, mathematical operations are required for many applications. There are many commands and techniques for doing math in Bash. I hope after reading this article you can perform your necessary math calculations in Bash shell. Let me know if you face any issues while performing the arithmetic calculation.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"People_Also_Ask\"><\/span>People Also Ask<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Why_%E2%80%9Cvalue_too_great_for_base%E2%80%9D_error_occurs_in_arithmetic_calculation_in_Bash\"><\/span>Why \u201cvalue too great for base\u201d error occurs in arithmetic calculation in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The &#8220;value too great for base&#8221; error in Bash occurs when someone tries to perform arithmetic calculations using a base that is not appropriate for the numbers involved. For instance, in the expression <code class=\"\" data-line=\"\">echo $((2#1010 + 2#6))<\/code>, the error arises because the number <strong>6 <\/strong>in base-2 (2#6) exceeds the binary representation, causing an arithmetic overflow. In this case, the base specified cannot accommodate the numeric value provided, leading to the error.<\/p>\n<p style=\"text-align: justify;\">To resolve this, it&#8217;s important to ensure that the base used in arithmetic calculations can represent the numbers involved without exceeding its capacity.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_solve_the_%E2%80%9Csyntax_error_invalid_arithmetic_operator%E2%80%9D_in_Bash\"><\/span>How to solve the \u201csyntax error: invalid arithmetic operator\u201d in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">\u201csyntax error: invalid arithmetic operator\u201d typically occurs when there is an issue in the arithmetic expression or the capability of the command used for evaluating the expression. To solve the error use a command based on the nature of the expression. Let\u2019s say you are calculating with floating point numbers but using arithmetic expansion to do that. This raises the error. To evaluate such expressions choose a command that can handle floating point arithmetic.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_solve_the_%E2%80%9Cnon-integer_argument%E2%80%9D_error_in_Bash\"><\/span>How to solve the \u201cnon-integer argument\u201d error in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To solve the \u201cnon-integer argument\u201d issue, provide an integer number instead of a floating point number. This issue often occurs when someone tries to evaluate an expression involving a floating point number with the <strong>expr<\/strong> command.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_can_I_generate_random_numbers_for_mathematical_calculation_in_Bash\"><\/span>How can I generate random numbers for mathematical calculation in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To generate random numbers in Bash use the <strong>RANDOM <\/strong>environment variable. One can also set the limits to get random numbers within a defined range. For example, to generate a random integer within the range of 1 to 100, use the following code:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo $((RANDOM % 100 + 1)<\/code><\/pre>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"Why \u201cvalue too great for base\u201d error occurs in arithmetic calculation in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The \\\"value too great for base\\\" error in Bash occurs when someone tries to perform arithmetic calculations using a base that is not appropriate for the numbers involved. For instance, in the expression echo $((2#1010 + 2#6)), the error arises because the number 6 in base-2 (2#6) exceeds the binary representation, causing an arithmetic overflow. In this case, the base specified cannot accommodate the numeric value provided, leading to the error.<\/p>\n<p>To resolve this, it's important to ensure that the base used in arithmetic calculations can represent the numbers involved without exceeding its capacity.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How to solve the \u201csyntax error: invalid arithmetic operator\u201d in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"\u201csyntax error: invalid arithmetic operator\u201d typically occurs when there is an issue in the arithmetic expression or the capability of the command used for evaluating the expression. To solve the error use a command based on the nature of the expression. Let\u2019s say you are calculating with floating point numbers but using arithmetic expansion to do that. This raises the error. To evaluate such expressions choose a command that can handle floating point arithmetic.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How to solve the \u201cnon-integer argument\u201d error in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To solve the \u201cnon-integer argument\u201d issue, provide an integer number instead of a floating point number. This issue often occurs when someone tries to evaluate an expression involving a floating point number with the expr command.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How can I generate random numbers for mathematical calculation in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To generate random numbers in Bash use the RANDOM environment variable. One can also set the limits to get random numbers within a defined range. For example, to generate a random integer within the range of 1 to 100, use the following code:<\/p>\n<p>echo $((RANDOM % 100 + 1)\"\n    }\n  }]\n}\n<\/script><br \/>\n<span style=\"font-size: 18pt; color: #003366;\"><strong>Related Articles<\/strong><\/span><\/p>\n<ul>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/calculate\/\" target=\"_blank\" rel=\"noopener\">How to Calculate in Bash [3 Practical Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/sum-numbers\/\" target=\"_blank\" rel=\"noopener\">How to Sum Up Numbers in Bash [Explained With Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/divide\/\" target=\"_blank\" rel=\"noopener\">How to Divide Two Numbers in Bash [8 Easy Ways]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/floating-point-division\/\" target=\"_blank\" rel=\"noopener\">Division of Floating Point Numbers in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/mod\/\" target=\"_blank\" rel=\"noopener\">How to Use &#8220;Mod&#8221; Operator in Bash [5 Basic Usage]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/format-number\/\" target=\"_blank\" rel=\"noopener\">How to Format Number in Bash [6 Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/random-number\/\" target=\"_blank\" rel=\"noopener\">How to Generate Random Number in Bash [7 Easy Ways]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/hex-to-decimal\/\" target=\"_blank\" rel=\"noopener\">How to Convert Hexadecimal Number to Decimal in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/binary-operator-expected\/\" target=\"_blank\" rel=\"noopener\">[Solved] \u201cbinary operator expected\u201d Error in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/integer-expression-expected\/\" target=\"_blank\" rel=\"noopener\">[Fixed] \u201cinteger expression expected\u201d Error in Bash<\/a><\/strong><\/li>\n<\/ul>\n<hr \/>\n<p><strong>&lt;&lt; Go Back to <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Arithmetic Operators in Bash&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/\" target=\"_blank\" rel=\"noopener\">Arithmetic Operators in Bash<\/a><\/span> | <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Operator&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/\" target=\"_blank\" rel=\"noopener\">Bash Operator<\/a><\/span> | <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\" target=\"_blank\" rel=\"noopener\">Bash Scripting Tutorial<\/a><\/strong><\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-center kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;center&quot;,&quot;id&quot;:&quot;28963&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Performing Math Operations in Bash [12+ Commands Explained]&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            5\/5 - (1 vote)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Mathematical or arithmetical calculations are essential for scheduling cron jobs, calculating the difference between the timestamps, and many other important &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Performing Math Operations in Bash [12+ Commands Explained]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#more-28963\" aria-label=\"Read more about Performing Math Operations in Bash [12+ Commands Explained]\">Read more<\/a><\/p>\n","protected":false},"author":314903,"featured_media":28997,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[170],"class_list":["post-28963","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-arithmetic-operators","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Performing Math Operations in Bash [12+ Commands Explained] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"One important task in Bash is to do math operations. This article talks about various commands to perform arithmetic calculations in Bash.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Performing Math Operations in Bash [12+ Commands Explained] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"One important task in Bash is to do math operations. This article talks about various commands to perform arithmetic calculations in Bash.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/zahidlaku72\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-29T11:07:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-16T09:16:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/math-in-bash.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Md Zahidul Islam Laku\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Md Zahidul Islam Laku\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"20 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/arithmetic-operators\\\/math-in-bash\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/arithmetic-operators\\\/math-in-bash\\\/\"},\"author\":{\"name\":\"Md Zahidul Islam Laku\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/26b37b2c7517d2c8702472bc3986ef21\"},\"headline\":\"Performing Math Operations in Bash [12+ Commands Explained]\",\"datePublished\":\"2023-11-29T11:07:32+00:00\",\"dateModified\":\"2024-01-16T09:16:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/arithmetic-operators\\\/math-in-bash\\\/\"},\"wordCount\":3095,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/arithmetic-operators\\\/math-in-bash\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/math-in-bash.png\",\"keywords\":[\"arithmetic operators\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/arithmetic-operators\\\/math-in-bash\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/arithmetic-operators\\\/math-in-bash\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/arithmetic-operators\\\/math-in-bash\\\/\",\"name\":\"Performing Math Operations in Bash [12+ Commands Explained] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/arithmetic-operators\\\/math-in-bash\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/arithmetic-operators\\\/math-in-bash\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/math-in-bash.png\",\"datePublished\":\"2023-11-29T11:07:32+00:00\",\"dateModified\":\"2024-01-16T09:16:41+00:00\",\"description\":\"One important task in Bash is to do math operations. This article talks about various commands to perform arithmetic calculations in Bash.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/arithmetic-operators\\\/math-in-bash\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/arithmetic-operators\\\/math-in-bash\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/arithmetic-operators\\\/math-in-bash\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/math-in-bash.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/math-in-bash.png\",\"width\":400,\"height\":400,\"caption\":\"math in bash\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/arithmetic-operators\\\/math-in-bash\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bash Scripting Tutorial\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Bash Operator\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Arithmetic Operators in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/operator\\\/arithmetic-operators\\\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Performing Math Operations in Bash [12+ Commands Explained]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"name\":\"LinuxSimply\",\"description\":\"All About Linux\",\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/linuxsimply.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\",\"name\":\"LinuxSimply\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"width\":355,\"height\":48,\"caption\":\"LinuxSimply\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/26b37b2c7517d2c8702472bc3986ef21\",\"name\":\"Md Zahidul Islam Laku\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Md-Zahidul-Islam-Laku-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Md-Zahidul-Islam-Laku-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Md-Zahidul-Islam-Laku-II-96x96.png\",\"caption\":\"Md Zahidul Islam Laku\"},\"description\":\"Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/zahidlaku72\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/zahidlaku\\\/\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCnxMIaAb-nXO5XnMeEaNhpQ\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/laku\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Performing Math Operations in Bash [12+ Commands Explained] - LinuxSimply","description":"One important task in Bash is to do math operations. This article talks about various commands to perform arithmetic calculations in Bash.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/","og_locale":"en_US","og_type":"article","og_title":"Performing Math Operations in Bash [12+ Commands Explained] - LinuxSimply","og_description":"One important task in Bash is to do math operations. This article talks about various commands to perform arithmetic calculations in Bash.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/zahidlaku72","article_published_time":"2023-11-29T11:07:32+00:00","article_modified_time":"2024-01-16T09:16:41+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/math-in-bash.png","type":"image\/png"}],"author":"Md Zahidul Islam Laku","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Md Zahidul Islam Laku","Est. reading time":"20 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/"},"author":{"name":"Md Zahidul Islam Laku","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/26b37b2c7517d2c8702472bc3986ef21"},"headline":"Performing Math Operations in Bash [12+ Commands Explained]","datePublished":"2023-11-29T11:07:32+00:00","dateModified":"2024-01-16T09:16:41+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/"},"wordCount":3095,"commentCount":1,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/math-in-bash.png","keywords":["arithmetic operators"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/","name":"Performing Math Operations in Bash [12+ Commands Explained] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/math-in-bash.png","datePublished":"2023-11-29T11:07:32+00:00","dateModified":"2024-01-16T09:16:41+00:00","description":"One important task in Bash is to do math operations. This article talks about various commands to perform arithmetic calculations in Bash.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/math-in-bash.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/math-in-bash.png","width":400,"height":400,"caption":"math in bash"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/math-in-bash\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxsimply.com\/"},{"@type":"ListItem","position":2,"name":"Bash Scripting Tutorial","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/"},{"@type":"ListItem","position":3,"name":"Bash Operator","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/"},{"@type":"ListItem","position":4,"name":"Arithmetic Operators in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/arithmetic-operators\/"},{"@type":"ListItem","position":5,"name":"Performing Math Operations in Bash [12+ Commands Explained]"}]},{"@type":"WebSite","@id":"https:\/\/linuxsimply.com\/#website","url":"https:\/\/linuxsimply.com\/","name":"LinuxSimply","description":"All About Linux","publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxsimply.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxsimply.com\/#organization","name":"LinuxSimply","url":"https:\/\/linuxsimply.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","width":355,"height":48,"caption":"LinuxSimply"},"image":{"@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/26b37b2c7517d2c8702472bc3986ef21","name":"Md Zahidul Islam Laku","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Md-Zahidul-Islam-Laku-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Md-Zahidul-Islam-Laku-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Md-Zahidul-Islam-Laku-II-96x96.png","caption":"Md Zahidul Islam Laku"},"description":"Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio","sameAs":["https:\/\/www.facebook.com\/zahidlaku72","https:\/\/www.linkedin.com\/in\/zahidlaku\/","https:\/\/www.youtube.com\/channel\/UCnxMIaAb-nXO5XnMeEaNhpQ"],"url":"https:\/\/linuxsimply.com\/author\/laku\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/28963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/users\/314903"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=28963"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/28963\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/28997"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=28963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=28963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=28963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}