{"id":910,"date":"2020-12-16T19:15:48","date_gmt":"2020-12-16T17:15:48","guid":{"rendered":"https:\/\/www.bashpi.org\/?page_id=910"},"modified":"2020-12-31T17:55:52","modified_gmt":"2020-12-31T15:55:52","slug":"bash-calculations","status":"publish","type":"page","link":"https:\/\/www.bashpi.org\/?page_id=910","title":{"rendered":"Bash calculations"},"content":{"rendered":"\n<p>Bash builtin calculations support only INTEGERS!<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>The shell allows arithmetic expressions to be evaluated, under certain circumstances (see the <strong>let<\/strong> and <strong>declare<\/strong> builtin commands, the <strong>((<\/strong> compound command, and <strong>Arithmetic<\/strong> <strong>Expansion<\/strong>). &nbsp;Evaluation is done in fixed-width &nbsp;integers &nbsp;with &nbsp;no &nbsp;check &nbsp;for &nbsp;overflow,&nbsp;though division by 0 is trapped and flagged as an error. &nbsp;The operators and their precedence, associativity, and values are the same as in the C language. <\/p><cite>from $ man bash<\/cite><\/blockquote>\n\n\n\n<p><strong>Division in bash<\/strong> &#8211; lets divide 5 with 2 and see what result we get.<\/p>\n\n\n\n<pre id=\"block-9d7c0986-41ef-43e6-8d7e-5c173bdbc165\" class=\"wp-block-preformatted\">$ let x=5\/2\n$ echo $x\n<strong>2<\/strong>\n<\/pre>\n\n\n\n<p><strong>Multiplication<\/strong> in bash<\/p>\n\n\n\n<pre id=\"block-df41c695-5366-4610-a848-1fe7ddc50804\" class=\"wp-block-preformatted\">$ let x=5*2\n$ echo $x\n<strong>10<\/strong><\/pre>\n\n\n\n<p><strong>Addition<\/strong> in bash, increment variable by one<\/p>\n\n\n\n<pre id=\"block-9d7c0986-41ef-43e6-8d7e-5c173bdbc165\" class=\"wp-block-preformatted\">$ let x=5+2\n$ echo $x\n<strong>7<\/strong>\n$ let x++\n<strong>8<\/strong><\/pre>\n\n\n\n<p><strong>Subtraction<\/strong> in bash \/ decrement variable by one<\/p>\n\n\n\n<pre id=\"block-9d7c0986-41ef-43e6-8d7e-5c173bdbc165\" class=\"wp-block-preformatted\">$ let x=5-2\n$ echo $x\n<strong>3<\/strong>\n$ let x-- \n<strong>2<\/strong><\/pre>\n\n\n\n<p><strong>Comparing floating point numbers<\/strong> <strong>in bash<\/strong><\/p>\n\n\n\n<p>Bash sucks in floating point number comparison.  <\/p>\n\n\n\n<p>Try these out as an example:<\/p>\n\n\n\n<pre id=\"block-c0699a83-50a6-48bd-be48-e378d8a06ad8\" class=\"wp-block-preformatted\">$ x=<strong>2.5<\/strong>;y=3;if [ $x <strong>&gt;<\/strong> $y ]; then echo \"x is greater than y\"; else echo \"x is smaller than y\";fi \nx is <strong>greater<\/strong> than y\n\n$ x=<strong>5.5<\/strong>;y=3;if [ $x -gt $y ]; then echo \"x is greater than y\"; else echo \"x is smaller than y\";fi \n<strong>bash&nbsp;error<\/strong> - integer expected (evaluates false)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\nx is <strong>smaller<\/strong> than y<\/pre>\n\n\n\n<p>To overcome the limitation use simple function and awk in your bash script. <\/p>\n\n\n\n<p>Sample script <strong>test.sh<\/strong> containing the <strong>awk<\/strong> part in a re-usable function named <strong>float<\/strong> and also usage example in the end.<\/p>\n\n\n\n<pre id=\"block-9d7c0986-41ef-43e6-8d7e-5c173bdbc165\" class=\"wp-block-preformatted\">#!\/bin\/bash \n \n# this function helps to compare floating point numbers &nbsp;\nfloat() { &nbsp;\n &nbsp;&nbsp;&nbsp;if [ $# -ne 2 ]; then &nbsp;\n &nbsp;\n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \"incorrect number of arguments\" &nbsp;\n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit 1 &nbsp;\n &nbsp;\n &nbsp;&nbsp;&nbsp;else &nbsp;\n &nbsp;\n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# will give both floats to awk as variables and let the awk do the comparison &nbsp;\n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;awk -v f1=${1} -v f2=${2} 'BEGIN{ if (f1&lt;f2) print \"less\"; if (f1==f2) print \"equal\"; if (f1&gt;f2) print \"more\"; }' &nbsp;\n &nbsp;\n &nbsp;&nbsp;&nbsp;fi &nbsp;\n} \n \n \nif [ \"`float ${1} ${2}`\" = \"less\" ];then \n &nbsp;&nbsp;echo \"${1} is less than ${2}\" \nelif [ \"`float ${1} ${2}`\" = \"more\" ];then \n &nbsp;&nbsp;echo \"${1} is greater than ${2}\" \nelse \n &nbsp;&nbsp;echo \"${1} is equal to ${2}\" \nfi\n \n\n<\/pre>\n\n\n\n<p>Invoking the script:<\/p>\n\n\n\n<pre id=\"block-60800385-cfcb-4541-b8f1-5216def00139\" class=\"wp-block-preformatted\">$ chmod 700 test.sh &nbsp;\n$ .\/test.sh 3 5 \n3 is less than 5 \n$ .\/test.sh 5 3.5 \n5 is greater than 3.5 \n$ .\/test.sh 2.5678734 2.56787 \n2.5678734 is greater than 2.56787 \n$ .\/test.sh 2.5638734 2.56787 &nbsp;\n2.5638734 is less than 2.56787\n<\/pre>\n\n\n\n<p>Works as a charm!<\/p>\n\n\n\n<p>Check also how to <a href=\"https:\/\/www.bashpi.org\/?page_id=779\" data-type=\"page\" data-id=\"779\">calculate pi in bash<\/a>.<\/p>\n\n\n\n<p>If you found this useful, say thanks, click on some banners or <a href=\"http:\/\/www.bashpi.org\/?page_id=105\">donate<\/a>, I can always use some beer money.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bash builtin calculations support only INTEGERS! The shell allows arithmetic expressions to be evaluated, under certain circumstances (see the let and declare builtin commands, the (( compound command, and Arithmetic Expansion). &nbsp;Evaluation is done in fixed-width &nbsp;integers &nbsp;with &nbsp;no &nbsp;check &nbsp;for &nbsp;overflow,&nbsp;though division by 0 is trapped and flagged as an error. &nbsp;The operators and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-910","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=910"}],"version-history":[{"count":2,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/910\/revisions"}],"predecessor-version":[{"id":1095,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/910\/revisions\/1095"}],"wp:attachment":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}