🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 103 (from laksa002)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

📄
INDEXABLE
CRAWLED
1 day ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.1 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://www.geeksforgeeks.org/linux-unix/bash-scripting-for-loop/
Last Crawled2026-04-07 12:02:40 (1 day ago)
First Indexed2025-06-13 06:41:45 (10 months ago)
HTTP Status Code200
Meta TitleBash Scripting - For Loop - GeeksforGeeks
Meta DescriptionYour All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more., Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Meta Canonicalnull
Boilerpipe Text
Last Updated : 15 Sep, 2023 Since BASH is a command-line language, we get some pretty feature-rich experience to leverage the programming skills to perform tasks in the terminal. We can use loops and conditional statements in BASH scripts to perform some repetitive and tricky problems in a simple programmatic way. In this article, we are going to focus on the for loop in BASH scripts. Depending on the use case and the problem it is trying to automate, there are a couple of ways to use loops. Simple For loop Range-based for loop Array iteration for loops C-Styled for loops Infinite for loop Simple For loop To execute a for loop we can write the following syntax: #!/bin/bash for n in a b c; do echo $n done In the first iteration, n takes the value "a", and the script prints "a". In the second iteration, n takes the value "b", and the script prints "b". In the third iteration, n takes the value "c", and the script prints "c". The above command will iterate over the specified elements after the in keyword one by one. The elements can be numbers, strings, or other forms of data. Range-based for loop We can use range-based for loops. In this type of loop, we can specify the number to start, to stop, and to increment at every iteration(optional) in the statement. There are two ways you can do this i.e. by mentioning the increment/decrementer value and by incrementing by one by default. The syntax looks like this: #!/bin/bash for n in {1..5}; do echo $n done In the above code, we use the "{}" to specify a range of numbers. Inside the curly braces, we specify the start point followed by two dots and an endpoint. By default, it increments by one. Hence we print 5 numbers from 1 to 5 both inclusive.  #!/bin/bash for n in {1..5..2}; do echo $n done Here we can see that the loop incremented by 2 units as mentioned in the curly braces. Thus, this makes working with numbers very easy and convenient. This can also be used with alphabetical characters.  NOTE: We cannot use variables inside the curly braces, so we will have to hardcode the values. To use the variables, we see the traditional C-styled for loops in the next few sections. Array iteration for loops We can iterate over arrays conveniently in bash using for loops with a specific syntax. We can use the special variables in BASH i.e @ to access all the elements in the array. Let's look at the code: #!/bin/bash s=("football" "cricket" "hockey") for n in ${s[@]}; do echo $n done We can iterate over the array elements using the @ operator that gets all the elements in the array. Thus using the for loop we iterate over them one by one. We use the variable ${variable_name[@]} in which, the curly braces here expand the value of the variable "s" here which is an array of strings. Using the [@] operator we access all the elements and thus iterate over them in the for a loop. Here, the " n " is the iterator hence, we can print the value or do the required processing on it.  C-Styled for loops As said earlier, we need to use the variables inside the for loops to iterate over a range of elements. And thus, the C-styled for loops play a very important role. Let's see how we use them. #!/bin/bash n=7 for (( i=1 ; i<=$n ; i++ )); do echo $i done As we can see we can dynamically use the value of the range of end conditions. Remember the spaces between the double braces might be intentional and are part of the syntax. C-styled for loops are the loops that have 3 parts, the initializing iterator, the incrementor/decrementer, and the end condition.  In the syntax above, we have initialized the loop iterator/counter to 1 that can be anything as per choice. The second part is the end condition, here we have used the variable "n" which is initialized before the for loop and thus we use the simple $ operator to get the value of the variable. Finally, we have the incrementor/decrementer which changes the iterator/counter to a value that can be anything but, in the example, we have used the unary operator (++) to increment the value by one which is equivalent to "i=i+1" . Thus we can use statements like i+=2, i--,++i, and so on and so forth. Infinite for loop We don't use this often but it is sometimes useful to get certain things working. The syntax is quite easy and similar to the C-styled for loops. #!/bin/bash n=4 for (( ; ; )); do if [ $n -eq 9 ];then break fi echo $n ((n=n+1)) done The loop starts with n set to 4. It increments n by 1 at each iteration and prints the value of n until n becomes equal to 9. When n reaches 9, the break statement is executed, and the loop terminates. The script stops after printing the numbers 4 to 8. As we can see the `for` loop has no conditions and this loops forever but we have a condition statement to check that it doesn't go on forever.  We use the break statement inside the if statement so as to get out of the loop and stop iterating with the iterator. We have used the incrementor to increment the variable in the loop otherwise the loop is infinite. Of course, we need some logic to break out of the loop and that is why we need to use the if conditional statement. 
Markdown
[![geeksforgeeks](https://media.geeksforgeeks.org/gfg-gg-logo.svg)](https://www.geeksforgeeks.org/) ![search icon](https://media.geeksforgeeks.org/auth-dashboard-uploads/Property=Light---Default.svg) - Sign In - [Courses]() - [Tutorials]() - [Interview Prep]() - [Linux-Unix](https://www.geeksforgeeks.org/linux-unix/linux-tutorial/) - [Interview Questions](https://www.geeksforgeeks.org/linux-unix/linux-interview-questions/) - [Shell Scripting](https://www.geeksforgeeks.org/linux-unix/introduction-linux-shell-shell-scripting/) - [Kali](https://www.geeksforgeeks.org/linux-unix/introduction-to-kali-linux/) - [Ubuntu](https://www.geeksforgeeks.org/linux-unix/how-to-install-ubuntu-on-virtualbox/) - [Red Hat](https://www.geeksforgeeks.org/tag/red-hat/) - [CentOS](https://www.geeksforgeeks.org/linux-unix/getting-started-with-centos/) - [Docker](https://www.geeksforgeeks.org/devops/introduction-to-docker/) - [Kubernetes](https://www.geeksforgeeks.org/devops/introduction-to-kubernetes-k8s/) - [Python](https://www.geeksforgeeks.org/python/python-programming-language-tutorial/) # Bash Scripting - For Loop Last Updated : 15 Sep, 2023 Since BASH is a command-line language, we get some pretty feature-rich experience to leverage the programming skills to perform tasks in the terminal. We can use loops and conditional statements in BASH scripts to perform some repetitive and tricky problems in a simple programmatic way. In this article, we are going to focus on the ****for loop**** in BASH scripts. Depending on the use case and the problem it is trying to automate, there are a couple of ways to use loops. - ****Simple For loop**** - ****Range-based for loop**** - ****Array iteration for loops**** - ****C-Styled for loops**** - ****Infinite for loop**** ## Simple For loop To execute a for loop we can write the following syntax: ``` #!/bin/bash for n in a b c; do echo $n done ``` - In the first iteration, `n` takes the value "a", and the script prints "a". - In the second iteration, `n` takes the value "b", and the script prints "b". - In the third iteration, `n` takes the value "c", and the script prints "c". ![501](https://media.geeksforgeeks.org/wp-content/uploads/20230726162232/501.png) The above command will iterate over the specified elements after the ****in**** keyword one by one. The elements can be numbers, strings, or other forms of data. ## Range-based for loop We can use range-based for loops. In this type of loop, we can specify the number to start, to stop, and to increment at every iteration(optional) in the statement. There are two ways you can do this i.e. by mentioning the increment/decrementer value and by incrementing by one by default. The syntax looks like this: ``` #!/bin/bash for n in {1..5}; do echo $n done ``` ![500](https://media.geeksforgeeks.org/wp-content/uploads/20230726162035/500.png) In the above code, we use the "{}" to specify a range of numbers. Inside the curly braces, we specify the start point followed by two dots and an endpoint. By default, it increments by one. Hence we print 5 numbers from 1 to 5 both inclusive. ``` #!/bin/bash for n in {1..5..2}; do echo $n done ``` ![502](https://media.geeksforgeeks.org/wp-content/uploads/20230726162440/502.png) Here we can see that the loop incremented by 2 units as mentioned in the curly braces. Thus, this makes working with numbers very easy and convenient. This can also be used with alphabetical characters. ****NOTE:**** We cannot use variables inside the curly braces, so we will have to hardcode the values. To use the variables, we see the traditional C-styled for loops in the next few sections. ## Array iteration for loops We can iterate over arrays conveniently in bash using for loops with a specific syntax. We can use the special variables in BASH i.e @ to access all the elements in the array. Let's look at the code: ``` #!/bin/bash s=("football" "cricket" "hockey") for n in ${s[@]}; do echo $n done ``` ![503](https://media.geeksforgeeks.org/wp-content/uploads/20230726162725/503.png) We can iterate over the array elements using the @ operator that gets all the elements in the array. Thus using the for loop we iterate over them one by one. We use the variable \${variable\_name\[@\]} in which, the curly braces here expand the value of the variable "s" here which is an array of strings. Using the \[@\] operator we access all the elements and thus iterate over them in the for a loop. Here, the "****n****" is the iterator hence, we can print the value or do the required processing on it. ## C-Styled for loops As said earlier, we need to use the variables inside the for loops to iterate over a range of elements. And thus, the C-styled for loops play a very important role. Let's see how we use them. ``` #!/bin/bash n=7 for (( i=1 ; i<=$n ; i++ )); do echo $i done ``` ![504](https://media.geeksforgeeks.org/wp-content/uploads/20230726162838/504.png) As we can see we can dynamically use the value of the range of end conditions. Remember the spaces between the double braces might be intentional and are part of the syntax. C-styled for loops are the loops that have 3 parts, the initializing iterator, the incrementor/decrementer, and the end condition. In the syntax above, we have initialized the loop iterator/counter to 1 that can be anything as per choice. The second part is the end condition, here we have used the variable ****"n"**** which is initialized before the for loop and thus we use the simple ****\$**** operator to get the value of the variable. Finally, we have the incrementor/decrementer which changes the iterator/counter to a value that can be anything but, in the example, we have used the unary operator ****(++)**** to increment the value by one which is equivalent to ****"i=i+1"****. Thus we can use statements like ****i+=2, i--,++i,**** and so on and so forth. ## Infinite for loop We don't use this often but it is sometimes useful to get certain things working. The syntax is quite easy and similar to the C-styled for loops. ``` #!/bin/bash n=4 for (( ; ; )); do if [ $n -eq 9 ];then break fi echo $n ((n=n+1)) done ``` The loop starts with `n` set to 4. It increments `n` by 1 at each iteration and prints the value of `n` until `n` becomes equal to 9. When `n` reaches 9, the `break` statement is executed, and the loop terminates. The script stops after printing the numbers 4 to 8. ![505](https://media.geeksforgeeks.org/wp-content/uploads/20230726162957/505.png) As we can see the \`for\` loop has no conditions and this loops forever but we have a condition statement to check that it doesn't go on forever. We use the break statement inside the if statement so as to get out of the loop and stop iterating with the iterator. We have used the incrementor to increment the variable in the loop otherwise the loop is infinite. Of course, we need some logic to break out of the loop and that is why we need to use the if conditional statement. Comment [M](https://www.geeksforgeeks.org/user/meetgor/) [meetgor](https://www.geeksforgeeks.org/user/meetgor/) 13 Article Tags: Article Tags: [Linux-Unix](https://www.geeksforgeeks.org/category/linux-unix/) [Bash-Script](https://www.geeksforgeeks.org/tag/bash-script/) ### Explore [![GeeksforGeeks](https://media.geeksforgeeks.org/auth-dashboard-uploads/gfgFooterLogo.png)](https://www.geeksforgeeks.org/) ![location](https://media.geeksforgeeks.org/img-practice/Location-1685004904.svg) Corporate & Communications Address: A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305) ![location](https://media.geeksforgeeks.org/img-practice/Location-1685004904.svg) Registered Address: K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305 [![GFG App on Play Store](https://media.geeksforgeeks.org/auth-dashboard-uploads/googleplay-%281%29.png)](https://geeksforgeeksapp.page.link/gfg-app)[![GFG App on App Store](https://media.geeksforgeeks.org/auth-dashboard-uploads/appstore-%281%29.png)](https://geeksforgeeksapp.page.link/gfg-app) - Company - [About Us](https://www.geeksforgeeks.org/about/) - [Legal](https://www.geeksforgeeks.org/legal/) - [Privacy Policy](https://www.geeksforgeeks.org/legal/privacy-policy/) - [Contact Us](https://www.geeksforgeeks.org/about/contact-us/) - [Advertise with us](https://www.geeksforgeeks.org/advertise-with-us/) - [GFG Corporate Solution](https://www.geeksforgeeks.org/gfg-corporate-solution/) - [Campus Training Program](https://www.geeksforgeeks.org/campus-training-program/) - Explore - [POTD](https://www.geeksforgeeks.org/problem-of-the-day) - [Job-A-Thon](https://practice.geeksforgeeks.org/events/rec/job-a-thon/) - [Blogs](https://www.geeksforgeeks.org/category/blogs/?type=recent) - [Nation Skill Up](https://www.geeksforgeeks.org/nation-skill-up/) - Tutorials - [Programming Languages](https://www.geeksforgeeks.org/computer-science-fundamentals/programming-language-tutorials/) - [DSA](https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/) - [Web Technology](https://www.geeksforgeeks.org/web-tech/web-technology/) - [AI, ML & Data Science](https://www.geeksforgeeks.org/machine-learning/ai-ml-and-data-science-tutorial-learn-ai-ml-and-data-science/) - [DevOps](https://www.geeksforgeeks.org/devops/devops-tutorial/) - [CS Core Subjects](https://www.geeksforgeeks.org/gate/gate-exam-tutorial/) - [Interview Preparation](https://www.geeksforgeeks.org/aptitude/interview-corner/) - [Software and Tools](https://www.geeksforgeeks.org/websites-apps/software-and-tools-a-to-z-list/) - Courses - [ML and Data Science](https://www.geeksforgeeks.org/courses/category/machine-learning-data-science) - [DSA and Placements](https://www.geeksforgeeks.org/courses/category/dsa-placements) - [Web Development](https://www.geeksforgeeks.org/courses/category/development-testing) - [Programming Languages](https://www.geeksforgeeks.org/courses/category/programming-languages) - [DevOps & Cloud](https://www.geeksforgeeks.org/courses/category/cloud-devops) - [GATE](https://www.geeksforgeeks.org/courses/category/gate) - [Trending Technologies](https://www.geeksforgeeks.org/courses/category/trending-technologies/) - Videos - [DSA](https://www.geeksforgeeks.org/videos/category/sde-sheet/) - [Python](https://www.geeksforgeeks.org/videos/category/python/) - [Java](https://www.geeksforgeeks.org/videos/category/java-w6y5f4/) - [C++](https://www.geeksforgeeks.org/videos/category/c/) - [Web Development](https://www.geeksforgeeks.org/videos/category/web-development/) - [Data Science](https://www.geeksforgeeks.org/videos/category/data-science/) - [CS Subjects](https://www.geeksforgeeks.org/videos/category/cs-subjects/) - Preparation Corner - [Interview Corner](https://www.geeksforgeeks.org/interview-prep/interview-corner/) - [Aptitude](https://www.geeksforgeeks.org/aptitude/aptitude-questions-and-answers/) - [Puzzles](https://www.geeksforgeeks.org/aptitude/puzzles/) - [GfG 160](https://www.geeksforgeeks.org/courses/gfg-160-series) - [System Design](https://www.geeksforgeeks.org/system-design/system-design-tutorial/) [@GeeksforGeeks, Sanchhaya Education Private Limited](https://www.geeksforgeeks.org/), [All rights reserved](https://www.geeksforgeeks.org/copyright-information/) ![]()
Readable Markdown
Last Updated : 15 Sep, 2023 Since BASH is a command-line language, we get some pretty feature-rich experience to leverage the programming skills to perform tasks in the terminal. We can use loops and conditional statements in BASH scripts to perform some repetitive and tricky problems in a simple programmatic way. In this article, we are going to focus on the ****for loop**** in BASH scripts. Depending on the use case and the problem it is trying to automate, there are a couple of ways to use loops. - ****Simple For loop**** - ****Range-based for loop**** - ****Array iteration for loops**** - ****C-Styled for loops**** - ****Infinite for loop**** ## Simple For loop To execute a for loop we can write the following syntax: ``` #!/bin/bashfor n in a b c; do echo $n done ``` - In the first iteration, `n` takes the value "a", and the script prints "a". - In the second iteration, `n` takes the value "b", and the script prints "b". - In the third iteration, `n` takes the value "c", and the script prints "c". ![501](https://media.geeksforgeeks.org/wp-content/uploads/20230726162232/501.png) The above command will iterate over the specified elements after the ****in**** keyword one by one. The elements can be numbers, strings, or other forms of data. ## Range-based for loop We can use range-based for loops. In this type of loop, we can specify the number to start, to stop, and to increment at every iteration(optional) in the statement. There are two ways you can do this i.e. by mentioning the increment/decrementer value and by incrementing by one by default. The syntax looks like this: ``` #!/bin/bashfor n in {1..5}; do echo $n done ``` ![500](https://media.geeksforgeeks.org/wp-content/uploads/20230726162035/500.png) In the above code, we use the "{}" to specify a range of numbers. Inside the curly braces, we specify the start point followed by two dots and an endpoint. By default, it increments by one. Hence we print 5 numbers from 1 to 5 both inclusive. ``` #!/bin/bashfor n in {1..5..2}; do echo $n done ``` ![502](https://media.geeksforgeeks.org/wp-content/uploads/20230726162440/502.png) Here we can see that the loop incremented by 2 units as mentioned in the curly braces. Thus, this makes working with numbers very easy and convenient. This can also be used with alphabetical characters. ****NOTE:**** We cannot use variables inside the curly braces, so we will have to hardcode the values. To use the variables, we see the traditional C-styled for loops in the next few sections. ## Array iteration for loops We can iterate over arrays conveniently in bash using for loops with a specific syntax. We can use the special variables in BASH i.e @ to access all the elements in the array. Let's look at the code: ``` #!/bin/bashs=("football" "cricket" "hockey") for n in ${s[@]}; do echo $n done ``` ![503](https://media.geeksforgeeks.org/wp-content/uploads/20230726162725/503.png) We can iterate over the array elements using the @ operator that gets all the elements in the array. Thus using the for loop we iterate over them one by one. We use the variable \${variable\_name\[@\]} in which, the curly braces here expand the value of the variable "s" here which is an array of strings. Using the \[@\] operator we access all the elements and thus iterate over them in the for a loop. Here, the "****n****" is the iterator hence, we can print the value or do the required processing on it. ## C-Styled for loops As said earlier, we need to use the variables inside the for loops to iterate over a range of elements. And thus, the C-styled for loops play a very important role. Let's see how we use them. ``` #!/bin/bashn=7 for (( i=1 ; i<=$n ; i++ )); do echo $i done ``` ![504](https://media.geeksforgeeks.org/wp-content/uploads/20230726162838/504.png) As we can see we can dynamically use the value of the range of end conditions. Remember the spaces between the double braces might be intentional and are part of the syntax. C-styled for loops are the loops that have 3 parts, the initializing iterator, the incrementor/decrementer, and the end condition. In the syntax above, we have initialized the loop iterator/counter to 1 that can be anything as per choice. The second part is the end condition, here we have used the variable ****"n"**** which is initialized before the for loop and thus we use the simple ****\$**** operator to get the value of the variable. Finally, we have the incrementor/decrementer which changes the iterator/counter to a value that can be anything but, in the example, we have used the unary operator ****(++)**** to increment the value by one which is equivalent to ****"i=i+1"****. Thus we can use statements like ****i+=2, i--,++i,**** and so on and so forth. ## Infinite for loop We don't use this often but it is sometimes useful to get certain things working. The syntax is quite easy and similar to the C-styled for loops. ``` #!/bin/bashn=4 for (( ; ; )); do if [ $n -eq 9 ];then break fi echo $n ((n=n+1)) done ``` The loop starts with `n` set to 4. It increments `n` by 1 at each iteration and prints the value of `n` until `n` becomes equal to 9. When `n` reaches 9, the `break` statement is executed, and the loop terminates. The script stops after printing the numbers 4 to 8. ![505](https://media.geeksforgeeks.org/wp-content/uploads/20230726162957/505.png) As we can see the \`for\` loop has no conditions and this loops forever but we have a condition statement to check that it doesn't go on forever. We use the break statement inside the if statement so as to get out of the loop and stop iterating with the iterator. We have used the incrementor to increment the variable in the loop otherwise the loop is infinite. Of course, we need some logic to break out of the loop and that is why we need to use the if conditional statement.
Shard103 (laksa)
Root Hash12046344915360636903
Unparsed URLorg,geeksforgeeks!www,/linux-unix/bash-scripting-for-loop/ s443