{"id":1869,"date":"2020-11-17T10:42:17","date_gmt":"2020-11-17T10:42:17","guid":{"rendered":"https:\/\/linuxdigest.com\/?p=1869"},"modified":"2020-12-13T17:11:17","modified_gmt":"2020-12-13T17:11:17","slug":"for-loops-in-bash","status":"publish","type":"post","link":"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/","title":{"rendered":"For loops in bash"},"content":{"rendered":"\n

You can use for loops in bash just like in any other programing language. The basic syntax is like this:<\/p>\n\n\n\n

for variable-name in 1 2 3\ndo\n  list-of-commands\ndone\n<\/code><\/pre>\n\n\n\n

One line for loop in bash<\/h2>\n\n\n\n

To do a quick one-liner with for, we use a semicolon to separate each line. In this example, we will give for a list of 4 items to iterate through.<\/p>\n\n\n\n

~$ for i in 1 2 3 4; do echo \"This is line number $i\"; done;\nThis is line number 1\nThis is line number 2\nThis is line number 3\nThis is line number 4\n<\/code><\/pre>\n\n\n\n

For loop in a bash script<\/h2>\n\n\n\n

When writing a script. We don’t need to use the semicolon as in the example above. This time we write each part of the command on a separate line. This example will print exactly the same output as the one-liner we just did.<\/p>\n\n\n\n

for i in 1 2 3 4\ndo\n        echo \"This is line number $i\"\ndone<\/code><\/pre>\n\n\n\n

Use a command to get a list of items<\/h2>\n\n\n\n

Instead of giving for a long list of items to iterate through, we can also wrap a command in $( ) to get the list. In this example we use the seq command to get a list of numbers 1 through 4. This will output exactly the same as above.<\/p>\n\n\n\n

for i in $( seq 1 4 )\ndo\n        echo \"This is line number $i\"\ndone\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

You can use for loops in bash just like in any other programing language. The basic…<\/p>\n","protected":false},"author":1,"featured_media":1919,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"yoast_head":"\nFor loops in bash - Linux Digest<\/title>\n<meta name=\"description\" content=\"The simple way to learn bash for loops.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"For loops in bash - Linux Digest\" \/>\n<meta property=\"og:description\" content=\"The simple way to learn bash for loops.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux Digest\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-17T10:42:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-13T17:11:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/11\/Copy-of-Untitled.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"700\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"The Linux Digest Guy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"The Linux Digest Guy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/\"},\"author\":{\"name\":\"The Linux Digest Guy\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2\"},\"headline\":\"For loops in bash\",\"datePublished\":\"2020-11-17T10:42:17+00:00\",\"dateModified\":\"2020-12-13T17:11:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/\"},\"wordCount\":163,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/linuxdigest.com\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/\",\"url\":\"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/\",\"name\":\"For loops in bash - Linux Digest\",\"isPartOf\":{\"@id\":\"https:\/\/linuxdigest.com\/#website\"},\"datePublished\":\"2020-11-17T10:42:17+00:00\",\"dateModified\":\"2020-12-13T17:11:17+00:00\",\"description\":\"The simple way to learn bash for loops.\",\"breadcrumb\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/linuxdigest.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"For loops in bash\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/linuxdigest.com\/#website\",\"url\":\"https:\/\/linuxdigest.com\/\",\"name\":\"Linux Digest\",\"description\":\"Linux tutorials for everyone\",\"publisher\":{\"@id\":\"https:\/\/linuxdigest.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/linuxdigest.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/linuxdigest.com\/#organization\",\"name\":\"Linux Digest\",\"url\":\"https:\/\/linuxdigest.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png\",\"contentUrl\":\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png\",\"width\":1102,\"height\":170,\"caption\":\"Linux Digest\"},\"image\":{\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2\",\"name\":\"The Linux Digest Guy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g\",\"caption\":\"The Linux Digest Guy\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"For loops in bash - Linux Digest","description":"The simple way to learn bash for loops.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/","og_locale":"en_US","og_type":"article","og_title":"For loops in bash - Linux Digest","og_description":"The simple way to learn bash for loops.","og_url":"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/","og_site_name":"Linux Digest","article_published_time":"2020-11-17T10:42:17+00:00","article_modified_time":"2020-12-13T17:11:17+00:00","og_image":[{"width":900,"height":700,"url":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/11\/Copy-of-Untitled.jpg","type":"image\/jpeg"}],"author":"The Linux Digest Guy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"The Linux Digest Guy","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/#article","isPartOf":{"@id":"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/"},"author":{"name":"The Linux Digest Guy","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2"},"headline":"For loops in bash","datePublished":"2020-11-17T10:42:17+00:00","dateModified":"2020-12-13T17:11:17+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/"},"wordCount":163,"commentCount":0,"publisher":{"@id":"https:\/\/linuxdigest.com\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/","url":"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/","name":"For loops in bash - Linux Digest","isPartOf":{"@id":"https:\/\/linuxdigest.com\/#website"},"datePublished":"2020-11-17T10:42:17+00:00","dateModified":"2020-12-13T17:11:17+00:00","description":"The simple way to learn bash for loops.","breadcrumb":{"@id":"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/linuxdigest.com\/howto\/for-loops-in-bash\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxdigest.com\/"},{"@type":"ListItem","position":2,"name":"For loops in bash"}]},{"@type":"WebSite","@id":"https:\/\/linuxdigest.com\/#website","url":"https:\/\/linuxdigest.com\/","name":"Linux Digest","description":"Linux tutorials for everyone","publisher":{"@id":"https:\/\/linuxdigest.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxdigest.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxdigest.com\/#organization","name":"Linux Digest","url":"https:\/\/linuxdigest.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png","contentUrl":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png","width":1102,"height":170,"caption":"Linux Digest"},"image":{"@id":"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2","name":"The Linux Digest Guy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g","caption":"The Linux Digest Guy"}}]}},"_links":{"self":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1869"}],"collection":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/comments?post=1869"}],"version-history":[{"count":1,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1869\/revisions"}],"predecessor-version":[{"id":1870,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1869\/revisions\/1870"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/media\/1919"}],"wp:attachment":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/media?parent=1869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/categories?post=1869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/tags?post=1869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}