{"id":326,"date":"2021-01-13T12:00:00","date_gmt":"2021-01-13T17:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=326"},"modified":"2020-12-24T00:52:31","modified_gmt":"2020-12-24T05:52:31","slug":"for-loop-in-bash","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/for-loop-in-bash\/","title":{"rendered":"for loop in bash"},"content":{"rendered":"\n<p>For loops in bash a similar to commonly used languages such as Perl and C. bash loops do not use curly brackets for body demarcation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>clear\nfor i in 1 2 3 4 5 6 7 8 9\ndo\n\u00a0 \u00a0echo \"printing number $i\"\ndone<\/code><\/pre>\n\n\n\n<p>Loop start from the do statement and ends with the done statement. This code prints numbers as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printing number 1\nprinting number 2\nprinting number 3\nprinting number 4\nprinting number 5\nprinting number 6\nprinting number 7\nprinting number 8\nprinting number 9<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>j=1;\nfor i in 1 2 3 4 5 6 7 8 9\ndo\n\u00a0 \u00a0j=$j*$i\n\u00a0 \u00a0echo $j\ndone<\/code><\/pre>\n\n\n\n<p>You would expect this code to multiply numbers and print their results but this code concatenated the values instead. bash treats all variables as strings and there is no casting. The output of the program is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1*1\n1*1*2\n1*1*2*3\n1*1*2*3*4\n1*1*2*3*4*5\n1*1*2*3*4*5*6\n1*1*2*3*4*5*6*7\n1*1*2*3*4*5*6*7*8\n1*1*2*3*4*5*6*7*8*9<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>j=1;\nfor i in 1 2 3 4 5 6 7 8 9\ndo\n\u00a0 \u00a0j=`echo \"$j * $i\" | bc`\n\u00a0 \u00a0echo $j\ndone<\/code><\/pre>\n\n\n\n<p>This code actually does what we expected the previous one to do. It multiplies variables and prints the results. We used echo to write the mathematical equation e.g. 2 * 3, and then pipe the string to bc. bc is a commandline calculator and it returned the value 6 which was stored in the variable j as a string and then printed to the screen with the following line of code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n6\n24\n120\n720\n5040\n40320\n362880<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>For loops in bash a similar to commonly used languages such as Perl and C. bash loops do not use curly brackets for body demarcation. Example 1 Loop start from the do statement and ends with the done statement. This code prints numbers as follows: Example 2 You would expect this code to multiply numbers [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101,34],"tags":[94,35],"class_list":["post-326","post","type-post","status-publish","format-standard","hentry","category-bash","category-linux","tag-bash","tag-linux"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=326"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/326\/revisions"}],"predecessor-version":[{"id":327,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/326\/revisions\/327"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}