{"id":9904,"date":"2022-12-10T12:37:17","date_gmt":"2022-12-10T11:37:17","guid":{"rendered":"https:\/\/monodes.com\/predaelli\/?p=9904"},"modified":"2022-12-10T12:37:19","modified_gmt":"2022-12-10T11:37:19","slug":"learn-how-to-use-bash-for-loop-in-shell-scripts","status":"publish","type":"post","link":"https:\/\/monodes.com\/predaelli\/2022\/12\/10\/learn-how-to-use-bash-for-loop-in-shell-scripts\/","title":{"rendered":"Learn How to Use Bash For Loop in Shell Scripts"},"content":{"rendered":"\n<p>A little promemoria <em><a href=\"https:\/\/www.tecmint.com\/bash-for-loop-linux\/\">For Loops in Shell Scripts<\/a><\/em><\/p>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bash-for-loop-with-ranges\">Bash For Loop with Ranges<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/bash\n\nfor n in {1..7};\ndo\n   echo $n\ndone<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/bash\n\nfor n in {1..7..2};\ndo\n   echo $n\ndone<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bash-for-loops-with-arrays\">Bash For Loops with Arrays<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/bash\n\nfruits=(\"blueberry\" \"peach\" \"mango\" \"pineapple\" \"papaya\") \n\nfor n in ${fruits[@]}; \ndo\n    echo $n\ndone\n\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bash-c-styled-for-loops\">Bash C-styled For Loops<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/bash\n\nn=7\nfor (( n=1 ; n&lt;=$n ; n++ )); \ndo\n    echo $n\ndone<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"use-the-continue-statement-with-bash-for-loop\">Use the \u2018Continue\u2019 statement with Bash For Loop<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/bash\nfor n in {1..10}\ndo\n        if [[ $n -eq '6' ]]\n        then\n              echo \"Target $n has been reached\"\n              continue\n        fi\n        echo $n\ndone<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"use-the-break-statement-with-bash-for-loop\">Use the \u2018break\u2019 statement with Bash For Loop<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/bash\nfor n in {1..10}\ndo\n        if [[ $n -eq '6' ]]\n        then\n                echo \"Target $n has been reached\"\n                break\n        fi\n        echo $n\ndone\necho \"All done\"\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">A little promemoria For Loops in Shell Scripts<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/monodes.com\/predaelli\/2022\/12\/10\/learn-how-to-use-bash-for-loop-in-shell-scripts\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[72],"tags":[260,298],"class_list":["post-9904","post","type-post","status-publish","format-standard","hentry","category-documentations","tag-bash","tag-shell"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6daft-2zK","jetpack-related-posts":[{"id":3766,"url":"https:\/\/monodes.com\/predaelli\/2018\/02\/08\/how-can-i-escape-white-space-in-a-bash-loop-list-stack-overflow\/","url_meta":{"origin":9904,"position":0},"title":"How can I escape white space in a bash loop list? &#8211; Stack Overflow","author":"Paolo Redaelli","date":"2018-02-08","format":"link","excerpt":"I find this nice and elegant. The other proposed solutions may handle files with newline in their name, but currently I see such file names as nonsense find . -type d | while read file; do echo $file; done However, doesn't work if the file-name contains newlines. The above is\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11554,"url":"https:\/\/monodes.com\/predaelli\/2024\/04\/03\/how-to-replace-string-in-bash-5-methods-linuxsimply\/","url_meta":{"origin":9904,"position":1},"title":"How to Replace String in Bash? [5 Methods] &#8211; LinuxSimply","author":"Paolo Redaelli","date":"2024-04-03","format":"quote","excerpt":"In bash scripting, the replacement of string is important because it allows you to modify a variable and the text of a file. It also helps in file and text processing and to validate user input. To replace a string in bash, check the following methods: Using Parameter expansion: ${String\/pattern\/replacement}\u2026","rel":"","context":"In &quot;Tricks&quot;","block_context":{"text":"Tricks","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/tricks\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":8844,"url":"https:\/\/monodes.com\/predaelli\/2021\/10\/18\/how-to-run-shell-script-as-systemd-service-in-linux\/","url_meta":{"origin":9904,"position":2},"title":"How to Run Shell Script as Systemd Service in Linux ?","author":"Paolo Redaelli","date":"2021-10-18","format":false,"excerpt":"How to Run Shell Script as Systemd Service in Linux ? Here, we will make a manual script which will act like a process to find disk utilization of the Linux system. To begin, Make a bash script that redirects date and disk utilization in a file. You can create\u2026","rel":"","context":"In &quot;Tricks&quot;","block_context":{"text":"Tricks","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/tricks\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7217,"url":"https:\/\/monodes.com\/predaelli\/2020\/05\/15\/copie-intelligenti\/","url_meta":{"origin":9904,"position":3},"title":"Copie intelligenti","author":"Paolo Redaelli","date":"2020-05-15","format":false,"excerpt":"Mi \u00e8 sempre piaciuta l'idea di avere copie \"intelligenti\", ossia una copia al giorno per una settimana una copia alla settimana per un meseuna copia al mese per un announa copia all'anno per la parte vecchia. E mi son sempre arrovellato su come implementarlo nel mio umile scriptino bash con\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3194,"url":"https:\/\/monodes.com\/predaelli\/2017\/07\/02\/3194\/","url_meta":{"origin":9904,"position":4},"title":"Ah, the magic of Bash!\u2026","author":"Paolo Redaelli","date":"2017-07-02","format":false,"excerpt":"Ah, the magic of Bash! for y in {1..30..7}; do for x in {60..1}; do DA=$(date -d \"-$(($x+$y)) days\" +%Y-%m-%d); A=$(date -d \"-$x days\" +%Y-%m-%d); echo -n \"Simulazione per $y giorni da $DA a $A: \"; .\/zenbot.sh sim --strategy trend_ema_reversed --start $DA --end $A | grep \"end balance\" --after-context=2 |tr\u2026","rel":"","context":"In &quot;Cryptocoins&quot;","block_context":{"text":"Cryptocoins","link":"https:\/\/monodes.com\/predaelli\/category\/cryptocoins\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":13137,"url":"https:\/\/monodes.com\/predaelli\/2025\/04\/18\/bssg-bash-static-site-generator\/","url_meta":{"origin":9904,"position":5},"title":"BSSG &#8211; Bash Static Site Generator","author":"Paolo Redaelli","date":"2025-04-18","format":false,"excerpt":"BSSG - Bash Static Site Generator A simple, performant static site generator in Bash for personal journals\/blogs with 50+ themes, i18n, SEO, parallel builds & more.","rel":"","context":"In &quot;Software Libero&quot;","block_context":{"text":"Software Libero","link":"https:\/\/monodes.com\/predaelli\/category\/software\/software-libero\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/9904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/comments?post=9904"}],"version-history":[{"count":0,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/9904\/revisions"}],"wp:attachment":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/media?parent=9904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/categories?post=9904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/tags?post=9904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}