{"id":8099,"date":"2019-01-13T04:49:14","date_gmt":"2019-01-13T04:49:14","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=8099"},"modified":"2019-01-23T23:28:54","modified_gmt":"2019-01-23T23:28:54","slug":"linux-today-5-useful-ways-to-do-arithmetic-in-linux-terminal","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/13\/linux-today-5-useful-ways-to-do-arithmetic-in-linux-terminal\/","title":{"rendered":"Linux Today &#8211; 5 Useful Ways to Do Arithmetic in Linux Terminal"},"content":{"rendered":"<p>In this article, we will show you various useful ways of doing arithmetic\u2019s in the Linux terminal. By the end of this article, you will learn basic different practical ways of doing mathematical calculations in the command line.<\/p>\n<p>Let\u2019s get started!<\/p>\n<h3>1. Using Bash Shell<\/h3>\n<p>The first and easiest way do basic math on the Linux CLI is a using double parenthesis. Here are some examples where we use values stored in variables:<\/p>\n<pre>$ ADD=$(( 1 + 2 ))\r\n$ echo $ADD\r\n$ MUL=$(( $ADD * 5 ))\r\n$ echo $MUL\r\n$ SUB=$(( $MUL - 5 ))\r\n$ echo $SUB\r\n$ DIV=$(( $SUB \/ 2 ))\r\n$ echo $DIV\r\n$ MOD=$(( $DIV % 2 ))\r\n$ echo $MOD\r\n<\/pre>\n<div id=\"attachment_31495\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/do-simple-math-in-cli-using-double-parenthesis.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31495\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/do-simple-math-in-cli-using-double-parenthesis.png\" alt=\"Arithmetic in Linux Bash Shell\" width=\"552\" height=\"325\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Arithmetic in Linux Bash Shell<\/p>\n<\/div>\n<h3>2. Using expr Command<\/h3>\n<p>The\u00a0<strong>expr command<\/strong>\u00a0evaluates expressions and prints the value of provided expression to standard output. We will look at different ways of using\u00a0<strong>expr<\/strong>\u00a0for doing simple math, making comparison, incrementing the value of a variable and finding the length of a string.<\/p>\n<p>The following are some examples of doing simple calculations using the\u00a0<strong>expr command<\/strong>. Note that many operators need to be escaped or quoted for shells, for instance the\u00a0<code>*<\/code>\u00a0operator (we will look at more under comparison of expressions).<\/p>\n<pre>$ expr 3 + 5\r\n$ expr 15 % 3\r\n$ expr 5 \\* 3\r\n$ expr 5 \u2013 3\r\n$ expr 20 \/ 4\r\n<\/pre>\n<div id=\"attachment_31496\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/do-simple-calculations-using-the-expr-command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31496\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/do-simple-calculations-using-the-expr-command.png\" alt=\"Basic Arithmetic Using expr Command in Linux\" width=\"482\" height=\"211\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Basic Arithmetic Using expr Command in Linux<\/p>\n<\/div>\n<p>Next, we will cover how to make comparisons. When an expression evaluates to false, expr will print a value of\u00a0<strong>0<\/strong>, otherwise it prints\u00a0<strong>1<\/strong>.<\/p>\n<p>Let\u2019s look at some examples:<\/p>\n<pre>$ expr 5 = 3\r\n$ expr 5 = 5\r\n$ expr 8 != 5\r\n$ expr 8 \\&gt; 5\r\n$ expr 8 \\&lt; 5\r\n$ expr 8 \\&lt;= 5\r\n<\/pre>\n<div id=\"attachment_31497\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/comparing-expressions.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31497\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/comparing-expressions.png\" alt=\"Comparing Arithmetic Expressions in Linux\" width=\"482\" height=\"249\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Comparing Arithmetic Expressions in Linux<\/p>\n<\/div>\n<p>You can also use the\u00a0<strong>expr<\/strong>\u00a0command to increment the value of a variable. Take a look at the following example (in the same way, you can also decrease the value of a variable).<\/p>\n<pre>$ NUM=$(( 1 + 2))\r\n$ echo $NUM\r\n$ NUM=$(expr $NUM + 2)\r\n$ echo $NUM\r\n<\/pre>\n<div id=\"attachment_31498\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/incrementing-value-of-a-variable.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31498\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/incrementing-value-of-a-variable.png\" alt=\"Increment Value of a Variable\" width=\"512\" height=\"135\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Increment Value of a Variable<\/p>\n<\/div>\n<p>Let\u2019s also look at how to find the length of a string using:<\/p>\n<pre>$ expr length \"This is Tecmint.com\"\r\n<\/pre>\n<div id=\"attachment_31499\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/find-length-of-a-string.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31499\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/find-length-of-a-string.png\" alt=\"Find Length of a String\" width=\"582\" height=\"59\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Find Length of a String<\/p>\n<\/div>\n<p>For more information especially on the meaning of the above operators, see the\u00a0<strong>expr<\/strong>\u00a0man page:<\/p>\n<pre>$ man expr\r\n<\/pre>\n<h3>3. Using bc Command<\/h3>\n<p><a href=\"https:\/\/www.tecmint.com\/bc-command-examples\/\" target=\"_blank\" rel=\"noopener\">bc (Basic Calculator)<\/a>\u00a0is a command-line utility that provides all features you expect from a simple scientific or financial calculator. It is specifically useful for doing floating point math.<\/p>\n<p>If\u00a0<strong>bc command<\/strong>\u00a0not installed, you can install it using:<\/p>\n<pre>$ sudo apt install bc   #Debian\/Ubuntu\r\n$ sudo yum install bc   #RHEL\/CentOS\r\n$ sudo dnf install bc   #Fedora 22+\r\n<\/pre>\n<p>Once installed, you can run it in interactive mode or non-interactively by passing arguments to it \u2013 we will look at both case. To run it interactively, type the command\u00a0<strong>bc<\/strong>\u00a0on command prompt and start doing some math, as shown.<\/p>\n<pre>$ bc \r\n<\/pre>\n<div id=\"attachment_31500\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/use-bc-non-interactively.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31500\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/use-bc-non-interactively.png\" alt=\"Start bc in Non-Interactive Mode\" width=\"502\" height=\"173\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Start bc in Non-Interactive Mode<\/p>\n<\/div>\n<p>The following examples show how to use\u00a0<strong>bc<\/strong>\u00a0non-interactively on the command-line.<\/p>\n<pre>$ echo '3+5' | bc\r\n$ echo '15 % 2' | bc\r\n$ echo '15 \/ 2' | bc\r\n$ echo '(6 * 2) - 5' | bc\r\n<\/pre>\n<div id=\"attachment_31501\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/doing-math-using-bc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31501\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/doing-math-using-bc.png\" sizes=\"auto, (max-width: 772px) 100vw, 772px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/doing-math-using-bc.png 772w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/doing-math-using-bc-768x380.png 768w\" alt=\"Do Math Using bc in Linux\" width=\"772\" height=\"382\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Do Math Using bc in Linux<\/p>\n<\/div>\n<p>The\u00a0<code>-l<\/code>\u00a0flag is used to the default scale (digits after the decimal point) to\u00a0<strong>20<\/strong>, for example:<\/p>\n<pre>$ echo '12\/5 | bc'\r\n$ echo '12\/5 | bc -l'\r\n<\/pre>\n<div id=\"attachment_31502\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/do-math-with-floating-point-numbers-in-bc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31502\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/do-math-with-floating-point-numbers-in-bc.png\" alt=\"Do Math with Floating Numbers\" width=\"532\" height=\"97\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Do Math with Floating Numbers<\/p>\n<\/div>\n<h3>4. Using Awk Command<\/h3>\n<p><a href=\"https:\/\/www.tecmint.com\/linux-awk-command-book-for-beginners\/\" target=\"_blank\" rel=\"noopener\">Awk<\/a>\u00a0is one of the most prominent text-processing programs in GNU\/Linux. It supports the addition, subtraction, multiplication, division, and modulus arithmetic operators. It is also useful for doing floating point math.<\/p>\n<p>You can use it to do basic math as shown.<\/p>\n<pre>$ awk 'BEGIN { a = 6; b = 2; print \"(a + b) = \", (a + b) }'\r\n$ awk 'BEGIN { a = 6; b = 2; print \"(a - b) = \", (a - b) }'\r\n$ awk 'BEGIN { a = 6; b = 2; print \"(a *  b) = \", (a * b) }'\r\n$ awk 'BEGIN { a = 6; b = 2; print \"(a \/ b) = \", (a \/ b) }'\r\n$ awk 'BEGIN { a = 6; b = 2; print \"(a % b) = \", (a % b) }'\r\n<\/pre>\n<div id=\"attachment_31503\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/do-basic-math-using-awk.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31503\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/do-basic-math-using-awk.png\" sizes=\"auto, (max-width: 832px) 100vw, 832px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/do-basic-math-using-awk.png 832w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/do-basic-math-using-awk-768x195.png 768w\" alt=\"Do Basic Math Using Awk Command\" width=\"832\" height=\"211\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Do Basic Math Using Awk Command<\/p>\n<\/div>\n<p>If you are new to\u00a0<strong>Awk<\/strong>, we have a complete series of guides to get you started with learning it:\u00a0<a href=\"https:\/\/www.tecmint.com\/category\/awk-command\/\" target=\"_blank\" rel=\"noopener\">Learn Awk Text Processing Tool<\/a>.<\/p>\n<h3>5. Using factor Command<\/h3>\n<p>The\u00a0<strong>factor command<\/strong>\u00a0is use to decompose an integer into prime factors. For example:<\/p>\n<pre>$ factor 10\r\n$ factor 127\r\n$ factor 222\r\n$ factor 110  \r\n<\/pre>\n<div id=\"attachment_31504\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/factor-a-number.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31504\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2019\/01\/factor-a-number.png\" alt=\"Factor a Number in Linux\" width=\"502\" height=\"173\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Factor a Number in Linux<\/p>\n<\/div>\n<p>That\u2019s all! In this article, we have explained various useful ways of doing arithmetic\u2019s in the Linux terminal.<\/p>\n<p><a href=\"http:\/\/www.linuxtoday.com\/developer\/5-useful-ways-to-do-arithmetic-in-linux-terminal-190108234012.html\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will show you various useful ways of doing arithmetic\u2019s in the Linux terminal. By the end of this article, you will learn basic different practical ways of doing mathematical calculations in the command line. Let\u2019s get started! 1. Using Bash Shell The first and easiest way do basic math on the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/13\/linux-today-5-useful-ways-to-do-arithmetic-in-linux-terminal\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Linux Today &#8211; 5 Useful Ways to Do Arithmetic in Linux Terminal&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8099","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=8099"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8099\/revisions"}],"predecessor-version":[{"id":8575,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8099\/revisions\/8575"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=8099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=8099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=8099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}