{"id":704,"date":"2016-03-01T21:47:05","date_gmt":"2016-03-01T21:47:05","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=704"},"modified":"2016-07-01T11:54:30","modified_gmt":"2016-07-01T11:54:30","slug":"command-line-fun-math","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/03\/01\/command-line-fun-math\/","title":{"rendered":"command line fun &#8211; math"},"content":{"rendered":"<p>What is nice about shell scripting on Linux is that the language is just as complete as most of the compiled languages.\u00a0 What elements makes up a pretty good computer language.<\/p>\n<ul>\n<li>variables<\/li>\n<li>assignments<\/li>\n<li>conditionals<\/li>\n<li>statements<\/li>\n<li>flow control<\/li>\n<li>expressions<\/li>\n<li>functions<\/li>\n<\/ul>\n<p>These elements are used in all computer languages from Lisp to C, and they all exist in bash script as well.\u00a0 Some of these elements also exist in windows batch files but not all.\u00a0 In Windows functions are a bit rudimentary and there is no math.<\/p>\n<p>There are a few ways to implement math in bash scripts.\u00a0 One of the methods that used to be used a long time ago is the external expr command.\u00a0 This command will evaluate integer expressions.\u00a0 The easiest way to see what this command can do is to try it on the command prompt.<\/p>\n<div class=\"sbody-code\">\n<pre><code>&gt; expr 3 \\* 3\r\n9\r\n<\/code><\/pre>\n<\/div>\n<p>The reason for the backslash is because we need to escape the asterisk from the shell.\u00a0 We need to do that for any character that could be interpreted by the shell.\u00a0 If you are using parenthesis then this has a tendency to make equations look pretty horrible.<\/p>\n<div class=\"sbody-code\">\n<pre><code>&gt; expr \\( 3 \\* 4 \\) \/ 2\r\n6<\/code><\/pre>\n<\/div>\n<p>Even if you can stand to look at your equations like this, you do have to be careful to include spaces between each element, failure to do so will yield the ever so helpful syntax error message.<\/p>\n<p>There is really no reason to use this command any longer as there are easier ways to do it in shell itself.\u00a0 The shell offers a easy syntax which allows you to enter the equation in a much more natural way.\u00a0 Simply enclose your equation with inside of a pair of parenthesis.<\/p>\n<p>$(( expression goes here ))<\/p>\n<div class=\"sbody-code\">\n<pre><code>&gt; echo $(( 5 * 17 \/ 7 ))\r\n12<\/code><\/pre>\n<\/div>\n<p>Not only is this easier to read, requires less care with respect to spaces but is also faster.\u00a0 It is faster as the overhead of external command is no longer necessary.<\/p>\n<p>Unfortunately there is a small drawback for both of these methods of evaluating expressions &#8211; they only support integer math.<\/p>\n<p>In those cases where you really need floating point math you will have to use an external program.\u00a0 The easiest choice is to use bc.\u00a0 It is possible set the number of decimal places using the scale variable.<\/p>\n<div class=\"sbody-code\">\n<pre><code>echo $(echo \"scale=4; 1 \/ 9 \" | bc )\r\n.1111\r\n<\/code><\/pre>\n<\/div>\n<p>This actually does the trick and if you are looking a specific number of decimal places this is perfect.\u00a0 If you are not looking for a specific number of decimal places, it is easier to use the &#8220;-l&#8221; option in conjunction with bc.\u00a0 This will load the mathlib but more important it will set the number of decimal places to 20.<\/p>\n<div class=\"sbody-code\">\n<pre><code>echo 1 \/ 9 | bc -l\r\n.11111111111111111111\r\n<\/code><\/pre>\n<\/div>\n<p>This is actually really easy to do simple equations but unfortunately it is not possible to use any functions in your equation.\u00a0 This makes sense as the functions are local to the shell while bc is an external program.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is nice about shell scripting on Linux is that the language is just as complete as most of the compiled languages.\u00a0 What elements makes up a pretty good computer language. variables assignments conditionals statements flow control expressions functions These &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/03\/01\/command-line-fun-math\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,20],"tags":[17,39],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/704"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=704"}],"version-history":[{"count":6,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/704\/revisions"}],"predecessor-version":[{"id":1052,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/704\/revisions\/1052"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}