đŸ•·ïž Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 143 (from laksa063)

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
20 hours ago
đŸ€–
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0 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.golinuxcloud.com/bash-for-loop/
Last Crawled2026-04-06 03:37:15 (20 hours ago)
First Indexed2020-05-23 08:40:14 (5 years ago)
HTTP Status Code200
Meta TitleBash For Loop usage guide for absolute beginners | GoLinuxCloud
Meta DescriptionFor loop in bash is used to iterate a certain command or task for a definite or infinite range. You can also use this in C programmer style, combine with continue and break statement
Meta Canonicalnull
Boilerpipe Text
I get this question a lot from many users, how can I run a certain command in a loop for a defined period of time? Or how can we iterate a task over a range only for n number of times. All these can be achieved using bash for loop in Linux and Unix From What is Loop , a loop is a sequence of instructions that is continually repeated until a certain condition is reached . So we know that a loop is a situation where we can perform a certain task repeatedly for a certain pre-defined period of time or may be some times infinite. Using such loops in our day to day task are very useful to automate stuffs using scripts. The loop can be configured using for, while, until etc depending upon individual’s requirement. In this tutorial we will understand in detail about bash for loop, and it’s usage across Linux environment for different types of automation shell scripts. I will also share different shell script examples using for loop to help you understand it’s usage. Basic for loop syntax in Bash The syntax of for loop would vary based on the programming language you choose such as C, perl, python, go etc. The provided syntax can be used only with bash and shell scripts for {ELEMENT} in ${ARRAY[@]} do {COMMAND} done Understanding the syntax Here the ARRAY can contain any type of element such as strings, integers. Although it is important that you pass an ARRAY to the loop, as if you pass a VARIABLE then the loop with count all the ELEMENTS from the VARIABLE as single ELEMENT . I will show you what this means later in this article Loop will iterate each ELEMENT from the ARRAY and a certain command can be assigned to these ELEMENT The loop will run until the last ELEMENT from ARRAY EX_1: Loop over a range of numbers In our first shell script we will iterate over a range of numbers and print the number for num in 1 2 3 4 5 6 do echo "Found Element: $num" done Here we have passed a series of numbers separated by white space The output from this script Found Element: 1 Found Element: 2 Found Element: 3 Found Element: 4 Found Element: 5 Found Element: 6 EX_2: Loop over a series of strings In this shell script we will iterate over a series of strings for string in STRING1 STRING2 STRING3 do echo "Found Element: $string" done The output from this script: Found Element: STRING1 Found Element: STRING2 Found Element: STRING3 Let us also take a practical example for Linux Administrator . In this script we will write a small script which will search for file1 , file2 and file3 under /tmp Now if you are not using for loop then you have to manually search for these files. You may think, so what it is easier to find files manually? Yes, TRUE but what if you have 100 files or 1000 files? Would you find them manually? for file in file1 file2 file3 do echo "Searching for file: $file" file_path=`find /tmp -name $file` if [ ! -z $file_path ];then echo "Found: $file at $file_path" else echo "Not Found: $file" fi done The output from the script will tell us the files which were found and the files which were not found. Bash for loop in a range EX_3: Use for loop with an array Before we go ahead it is important that you understand the different between ARRAY and Variable Array vs Variable It is important that when you use loop, you use an ARRAY which contains elements separated by white character A variable will always contain a single element while an array can contain multiple number of elements An array can also store multiple variables You can convert your VARIABLE into an ARRAY using different methods . For example: VAR="My name is Deepak" Here we have given white space separated values, so does this become an ARRAY ? NO, this is a VARIABLE Check the length of this VARIABLE echo "Length: ${#VAR[@]}" Length: 1 Bash could only count single element here, so if we try to iterate using this VAR then we get only 1 iteration You should know how to declare an array in shell script : Now I will declare the same content as an array VAR=(My name is Deepak) Here I have added the elements inside parenthesis , now let us check the length of this variable # echo "Length: ${#VAR[@]}" Length: 4 So now bash considers this as an ARRAY with 4 ELEMENTS 1st - My 2nd - name 3rd - is 4th - Deepak We can access these individual elements using ${VAR[$num]} Here $num should be replaced by the element number you want to access, for example : #!/bin/bash VAR=(My name is Deepak) echo "First Element: ${VAR[0]}" echo "First Element: ${VAR[1]}" echo "First Element: ${VAR[2]}" echo "First Element: ${VAR[3]}" The output of this script: Bash for loop array Let us take a practical example to understand loop with array in real time environment. Assume you have to create 5 users and assign a password to these users . In this shell script I will assign “ Passw0rd ” as password to all the users #!/bin/bash USERS=(user1 user2 user3 user4 user5) for user in ${USERS[@]} do echo "Creating user $user" useradd $user echo "Assigning password for $user" echo "Passw0rd" | passwd "$user" --stdin done EX_4: Using for loops in bash as C programmers Bash extends the for keyword to provide functionality similar to the three-argument for loop used in C using double parenthesis We can use the above examples when we have a small number of range to iterate over but what if we have a larger range of items ? Or if we do not know the number of times the loop has to run and instead we get this value internally from the script? Syntax: for ((ASSIGN_VALUE; ASSIGN_LIMIT ; STEP_UP)) do [COMMANDS] done The syntax would be more clear in this sample script. The first expression is run before the loop starts: We assign the i value to zero to start it off. The second expression is the test used to determine whether the loop should continue or stop: We test whether the value of i is less than 5. The third expression runs after each instance of the loop: We add one to the value of i . #!/bin/bash for (( i=0; i<=5; i++ )) do echo "Iteration $1" done The output from this script: Iteration 0 Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 Now you must be wondering do we have any use case for such of “for loop” in real time scenario . The answer would be YES , very much. I will share some real time scenarios to help you understand better: In this example I will search for all files with the syntax matching file* under /tmp Then append some content to these files Now the loop should iterate based on the number of files found Since the iteration starts from 0 we have given variable i to be one less than the number of files which is found #!/bin/bash file_path=(`find /tmp -name "file*" -type f`) echo "Found ${#file_path[@]} files" for (( i=1; i<${#file_path[@]}; i++ )) do echo "Adding content to ${file_path[$i]}" echo "add some content" >> "${file_path[$i]}" done The output from this script: bash for loop example Define multiple variables We can also use multiple variables in single for loop. In this example I have defined two variables where the loop will iterate until i is less than equal to j variable #!/bin/bash for (( i=1,j=5; i<=j; i++,j-- )) do echo "Iteration for i=$i" echo "Iteration for j=$j" done The output from this script Iteration for i=1 Iteration for j=5 Iteration for i=2 Iteration for j=4 Iteration for i=3 Iteration for j=3 In this script I will perform some more operations using two variables in single for loop with bash for (( i=0, j=0 ; i+j < 5 ; i++, j++ )) do echo "Value for i=$i" echo "Value for j=$j" echo "Multiply: $((i*j))" echo "add: $((i+j))" done The output from this script Value for i=0 Value for j=0 Multiply: 0 add: 0 Value for i=1 Value for j=1 Multiply: 1 add: 2 Value for i=2 Value for j=2 Multiply: 4 add: 4 EX_5: Use continue statement Now it is not mandatory that you would want to perform task for complete iteration, it is possible you may have a specific requirement to ignore certain value during the iteration run I have this sample script where the loop iterates for 5 times for (( i=0 ; i<=5 ; i++ )) do if [ $i -eq 2 ];then echo "Don't do anything when i=2" fi echo "Doing something when i=$i" done Now I have an additional check when value of the variable i is equal to 2 , so when this matches the script should do nothing and continue with the remaining iteration But let us check the output from this script Doing something when i=0 Doing something when i=1 Don't do anything when i=2 Doing something when i=2 Doing something when i=3 Doing something when i=4 Doing something when i=5 Even when I added a check to ignore i=2 match, still we see “ Doing something when i=2 ”, so how to fix this? We need to use continue statement for such case The continue statement will exit the current iteration of the loop and will continue with the next iteration in the loop for (( i=0 ; i<=5 ; i++ )) do if [ $i -eq 2 ];then echo "Don't do anything when i=2" continue fi echo "Doing something when i=$i" done The output from this script Doing something when i=0 Doing something when i=1 Don't do anything when i=2 Doing something when i=3 Doing something when i=4 Doing something when i=5 Now we see that when variable of i is equal to 2 , the iteration is skipped and the loop continues with remaining iteration EX_6: Use break statement With continue we were able to skip the loop for single iteration but what if we want to completely come out of the loop with a certain condition? In this script we want to end the loop if the value of i variable is equal to 2 so with the if condition I have added a break statement for (( i=0 ; i<=5 ; i++ )) do if [ $i -eq 2 ];then echo "Exiting, match sucessful" break fi echo "Doing something when i=$i" done The output from this script Doing something when i=0 Doing something when i=1 Exiting, match successful As expected, our for loop terminates when the if condition is fulfilled using the break statement. EX_7: One liner for loop examples We can write for loop in one liner commands to perform simple tasks using below syntax : for {ELEMENT} in ${ARRAY[@]}; do [COMMAND_1]; [COMMAND_2]; [COMMAND_3]; done In this shell script I will list all the network devices available on my server. here instead of defining an ARRAY , I have provided a PATH with * , so all the files and directories under that PATH would be considered as ELEMENT of the ARRAY # for int in `ls /proc/sys/net/ipv4/conf/`;do echo "Interface: $int"; done Interface: all Interface: bond0 Interface: bond1 Interface: default Interface: eth0 Interface: eth1 Interface: eth2 Interface: eth3 Interface: lo In this script I will remove all files under /tmp matching regex file* # for file in /tmp/file*; do rm -vf $file; done removed '/tmp/file1' removed '/tmp/file2' removed '/tmp/file3' Conclusion Even though bash is loosing it’s popularity with new programming language coming every day but engineers and developers still prefer bash to automate day to day tasks which consists of system commands. In such case it is always handy for engineers to be aware of using such loop based iteration. I have given some very basic examples for beginners to understand the syntax but this can be used to do complex tasks as well. Lastly I hope this tutorial to learn bash for loop with examples to iterate over a range and series of items on Linux and Unix was helpful. So, let me know your suggestions and feedback using the comment section.
Markdown
☰ [![GoLinuxCloud](https://www.golinuxcloud.com/images/golinuxcloud-logo.webp)](https://www.golinuxcloud.com/) [Start Here](https://www.golinuxcloud.com/blog/)[About](https://www.golinuxcloud.com/about-us/)[Contact](https://www.golinuxcloud.com/contact-us/)[Privacy](https://www.golinuxcloud.com/privacy-policy/)[Disclaimer](https://www.golinuxcloud.com/disclaimer/) ☰ â€č [CheatSheet](https://www.golinuxcloud.com/linux-commands-cheat-sheet/)[Ethical Hacking](https://www.golinuxcloud.com/kali-linux-virtualbox/)[Kubernetes](https://www.golinuxcloud.com/kubernetes-tutorial/)[Ansible](https://www.golinuxcloud.com/ansible-tutorial/)[Git](https://www.golinuxcloud.com/git-command-cheat-sheet/)[AWS](https://www.golinuxcloud.com/category/aws/)[Azure](https://www.golinuxcloud.com/create-vm-in-azure-step-by-step/)[OpenSSL](https://www.golinuxcloud.com/openssl-cheatsheet/)[OpenLDAP](https://www.golinuxcloud.com/ldap-tutorial-for-beginners-configure-linux/)[Wireshark](https://www.golinuxcloud.com/how-to-use-wireshark/)[Golang](https://www.golinuxcloud.com/getting-started-with-golang/)[Python](https://www.golinuxcloud.com/python-multiline-comments/)[Java](https://www.golinuxcloud.com/install-java-linux-windows-mac/)[JavaScript](https://www.golinuxcloud.com/javascript-global-variable/)[Node.js](https://www.golinuxcloud.com/getting-started-with-nodejs-tutorial/)[Laravel](https://www.golinuxcloud.com/laravel-installation/)[Pandas](https://www.golinuxcloud.com/python-pandas-tutorial/)[Shell Scripting](https://www.golinuxcloud.com/bash-if-else/)[SQL](https://www.golinuxcloud.com/getting-started-with-sql/)[Interview Questions](https://www.golinuxcloud.com/category/interview-questions/) â€ș - [Bash If Else](https://www.golinuxcloud.com/bash-if-else/ "Bash If Else") - [Bash Compare Numbers](https://www.golinuxcloud.com/bash-compare-numbers/ "Bash Compare Numbers") - [Bash Compare Strings](https://www.golinuxcloud.com/bash-compare-strings/ "Bash Compare Strings") - [Bash Concatenate Strings](https://www.golinuxcloud.com/bash-concatenate-strings/ "Bash Concatenate Strings") - [Bash Increment Variable](https://www.golinuxcloud.com/bash-increment-variable/ "Bash Increment Variable") - [Echo Command in Linux](https://www.golinuxcloud.com/echo-n-linux/ "Echo Command in Linux") - [Bash For Loop](https://www.golinuxcloud.com/bash-for-loop/ "Bash For Loop") - [Bash While Loop](https://www.golinuxcloud.com/bash-while-loop/ "Bash While Loop") - [Bash Until vs While Loop](https://www.golinuxcloud.com/bash-until-vs-while-loop/ "Bash Until vs While Loop") - [Run While Loop Until Specific Time](https://www.golinuxcloud.com/run-while-loop-until-specific-time-shell-bash/ "Run While Loop Until Specific Time") - [Bash Function](https://www.golinuxcloud.com/bash-function/ "Bash Function") - [Bash Script Arguments](https://www.golinuxcloud.com/bash-script-multiple-arguments/ "Bash Script Arguments") - [Script Arguments Guide](https://www.golinuxcloud.com/beginners-guide-to-use-script-arguments-in-bash-with-examples/ "Script Arguments Guide") - [Bash Getopts](https://www.golinuxcloud.com/bash-getopts/ "Bash Getopts") - [Split String into Array](https://www.golinuxcloud.com/bash-split-string-into-array-linux/ "Split String into Array") - [Delete Elements from Array](https://www.golinuxcloud.com/delete-elements-of-one-array-from-another-array/ "Delete Elements from Array") - [Unshift in Bash](https://www.golinuxcloud.com/unshift-in-bash/ "Unshift in Bash") - [Check String Contains Numbers](https://www.golinuxcloud.com/check-if-string-contains-numbers-shell-script/ "Check String Contains Numbers") - [Check if File Exists](https://www.golinuxcloud.com/bash-check-if-file-exists/ "Check if File Exists") - [Check if Script is Running](https://www.golinuxcloud.com/check-if-script-is-already-running-shell-linux/ "Check if Script is Running") - [Get Script Name and Path](https://www.golinuxcloud.com/get-script-name-get-script-path-shell-script/ "Get Script Name and Path") - [Get Script Execution Time](https://www.golinuxcloud.com/get-script-execution-time-command-bash-script/ "Get Script Execution Time") - [Count Word Occurrences](https://www.golinuxcloud.com/count-occurrences-of-word-in-file-bash-linux/ "Count Word Occurrences") - [Print Next Word After Pattern](https://www.golinuxcloud.com/print-next-word-after-pattern-match-linux/ "Print Next Word After Pattern") - [Add Thousands Separator](https://www.golinuxcloud.com/add-thousands-separator-shell-script-linux/ "Add Thousands Separator") - [Convert Space to Tab](https://www.golinuxcloud.com/use-tab-in-unix-shell-script-convert-space/ "Convert Space to Tab") - [Capture Ctrl+C in Bash](https://www.golinuxcloud.com/capture-ctrl-c-in-bash/ "Capture Ctrl+C in Bash") - [Run Scripts in Parallel](https://www.golinuxcloud.com/run-shell-scripts-in-parallel-collect-exit-status-process/ "Run Scripts in Parallel") - [Find Duplicate Files (Hash)](https://www.golinuxcloud.com/find-remove-duplicate-files-hash-shell-script/ "Find Duplicate Files (Hash)") - [Create Bash Progress Bar](https://www.golinuxcloud.com/create-bash-progress-bar/ "Create Bash Progress Bar") - [Create Users in Bulk](https://www.golinuxcloud.com/create-users-in-bulk-using-shell-script/ "Create Users in Bulk") - [Check CPU and Memory Usage](https://www.golinuxcloud.com/check-top-memory-cpu-consuming-process-script/ "Check CPU and Memory Usage") - [Loan EMI Calculator Script](https://www.golinuxcloud.com/calculate-loan-emi-interest-amount-bash-linux/ "Loan EMI Calculator Script") - [Interactive Calculator](https://www.golinuxcloud.com/interactive-calculator-shell-script-linux/ "Interactive Calculator") - [Bashrc vs Bash Profile](https://www.golinuxcloud.com/bashrc-vs-bash-profile/ "Bashrc vs Bash Profile") - [Linux Login History](https://www.golinuxcloud.com/linux-login-history/ "Linux Login History") # Bash For Loop usage guide for absolute beginners By [Deepak Prasad](https://www.golinuxcloud.com/author/admin/) Updated: Aug 4, 2020 In [programming](https://www.golinuxcloud.com/category/programming/) \| Tags: [\#bash](https://www.golinuxcloud.com/tag/bash/), [\#loop](https://www.golinuxcloud.com/tag/loop/), [\#shell script](https://www.golinuxcloud.com/tag/shell-script/) ![Bash For Loop usage guide for absolute beginners](https://www.golinuxcloud.com/bash-for-loop/for_loop-1-800w.webp) ## On this page - [Basic for loop syntax in Bash](https://www.golinuxcloud.com/bash-for-loop/#basic-for-loop-syntax-in-bash) - [Understanding the syntax](https://www.golinuxcloud.com/bash-for-loop/#understanding-the-syntax) - [EX\_1: Loop over a range of numbers](https://www.golinuxcloud.com/bash-for-loop/#ex_1-loop-over-a-range-of-numbers) - [EX\_2: Loop over a series of strings](https://www.golinuxcloud.com/bash-for-loop/#ex_2-loop-over-a-series-of-strings) - [EX\_3: Use for loop with an array](https://www.golinuxcloud.com/bash-for-loop/#ex_3-use-for-loop-with-an-array) - [Array vs Variable](https://www.golinuxcloud.com/bash-for-loop/#array-vs-variable) - [EX\_4: Using for loops in bash as C programmers](https://www.golinuxcloud.com/bash-for-loop/#ex_4-using-for-loops-in-bash-as-c-programmers) - [Define multiple variables](https://www.golinuxcloud.com/bash-for-loop/#define-multiple-variables) - [EX\_5: Use continue statement](https://www.golinuxcloud.com/bash-for-loop/#ex_5-use-continue-statement) - [EX\_6: Use break statement](https://www.golinuxcloud.com/bash-for-loop/#ex_6-use-break-statement) - [EX\_7: One liner for loop examples](https://www.golinuxcloud.com/bash-for-loop/#ex_7-one-liner-for-loop-examples) - [Conclusion](https://www.golinuxcloud.com/bash-for-loop/#conclusion) I get this question a lot from many users, how can I run a certain command in a loop for a defined period of time? Or how can we iterate a task over a range only for n number of times. All these can be achieved using bash for loop in Linux and Unix From [What is Loop](https://whatis.techtarget.com/definition/loop), **a loop is a sequence of instructions that is continually repeated until a certain condition is reached**. So we know that a loop is a situation where we can perform a certain task repeatedly for a certain pre-defined period of time or may be some times infinite. Using such loops in our day to day task are very useful to automate stuffs using scripts. The loop can be configured using for, while, until etc depending upon individual’s requirement. In this tutorial we will understand in detail about bash for loop, and it’s usage across Linux environment for different types of automation shell scripts. I will also share different shell script examples using for loop to help you understand it’s usage. *** ## Basic for loop syntax in Bash The syntax of for loop would vary based on the programming language you choose such as C, perl, python, go etc. The provided **syntax** can be used only with bash and shell scripts ``` for {ELEMENT} in ${ARRAY[@]} do {COMMAND} done ``` ### Understanding the syntax - Here the `ARRAY` can contain any type of element such as strings, integers. - Although it is important that **you pass an `ARRAY`** to the loop, as if you pass a `VARIABLE` then the loop with count all the `ELEMENTS` from the `VARIABLE` as single `ELEMENT`. *I will show you what this means later in this article* - Loop will iterate each `ELEMENT` from the `ARRAY` and a certain command can be assigned to these `ELEMENT` - The loop will run until the last `ELEMENT` from `ARRAY` *** ## EX\_1: Loop over a range of numbers In our first shell script we will iterate over a range of numbers and print the number ``` for num in 1 2 3 4 5 6 do echo "Found Element: $num" done ``` Here we have passed a series of numbers separated by white space The output from this script ``` Found Element: 1 Found Element: 2 Found Element: 3 Found Element: 4 Found Element: 5 Found Element: 6 ``` *** ## EX\_2: Loop over a series of strings In this shell script we will iterate over a series of strings ``` for string in STRING1 STRING2 STRING3 do echo "Found Element: $string" done ``` The output from this script: ``` Found Element: STRING1 Found Element: STRING2 Found Element: STRING3 ``` Let us also take a [practical example for Linux Administrator](https://www.golinuxcloud.com/linux-commands-cheat-sheet/). In this script we will write a small script which will search for `file1`, `file2` and `file3` under `/tmp` Now if you are not using for loop then you have to manually search for these files. **You may think, so what it is easier to find files manually?** Yes, **TRUE** but what if you have 100 files or 1000 files? Would you find them manually? ``` for file in file1 file2 file3 do echo "Searching for file: $file" file_path=`find /tmp -name $file` if [ ! -z $file_path ];then echo "Found: $file at $file_path" else echo "Not Found: $file" fi done ``` The output from the script will tell us the files which were found and the files which were not found. ![Beginners Tutorial to use Bash For Loop](https://www.golinuxcloud.com/bash-for-loop/for_loop.webp) *Bash for loop in a range* *** ## EX\_3: Use for loop with an array Before we go ahead it is important that you understand the different between ARRAY and Variable ### Array vs Variable - It is **important** that when you use loop, you use an `ARRAY` **which contains elements separated by white character** - A variable will always contain a single element while an array can contain multiple number of elements - An array can also store multiple variables - You can [convert your VARIABLE into an ARRAY using different methods](https://www.golinuxcloud.com/bash-split-string-into-array-linux/ "Bash split string into array using 4 simple methods"). *For example:* ``` VAR="My name is Deepak" ``` Here we have given white space separated values, so does this become an `ARRAY`? **NO, this is a VARIABLE** Check the length of this `VARIABLE` ``` echo "Length: ${#VAR[@]}" Length: 1 ``` Bash could only count single element here, so if we try to iterate using this `VAR` then we get only `1` iteration You should know **how to declare an array in shell script**: Now I will declare the same content as an array ``` VAR=(My name is Deepak) ``` Here I have added the elements inside *parenthesis*, now let us check the length of this variable ``` # echo "Length: ${#VAR[@]}" Length: 4 ``` So now bash considers this as an `ARRAY` with **4** `ELEMENTS` ``` 1st - My 2nd - name 3rd - is 4th - Deepak ``` We can **access these individual elements** using `${VAR[$num]}` Here `$num` should be replaced by the element number you want to access, *for example*: ``` #!/bin/bash VAR=(My name is Deepak) echo "First Element: ${VAR[0]}" echo "First Element: ${VAR[1]}" echo "First Element: ${VAR[2]}" echo "First Element: ${VAR[3]}" ``` The output of this script: ![Beginners Tutorial to use Bash For Loop](https://www.golinuxcloud.com/bash-for-loop/for_loop_1.webp) *Bash for loop array* Let us take a **practical example** to understand loop with array in real time environment. Assume you have to *create 5 users* and *assign a password to these users*. In this shell script I will assign “`Passw0rd`” as password to all the users ``` #!/bin/bash USERS=(user1 user2 user3 user4 user5) for user in ${USERS[@]} do echo "Creating user $user" useradd $user echo "Assigning password for $user" echo "Passw0rd" | passwd "$user" --stdin done ``` *** ## EX\_4: Using for loops in bash as C programmers - Bash extends the `for` keyword to provide functionality similar to the three-argument **for loop used in C using double parenthesis** - We can use the above examples when we have a small number of range to iterate over but what if we have a *larger range of items*? - Or if we do not know the number of times the loop has to run and instead we get this value internally from the script? **Syntax:** ``` for ((ASSIGN_VALUE; ASSIGN_LIMIT ; STEP_UP)) do [COMMANDS] done ``` - The **syntax** would be more clear in this sample script. - The **first expression** is run before the loop starts: We assign the `i` value to zero to start it off. - The **second expression** is the test used to determine whether the loop should continue or stop: We test whether the value of `i` is less than 5. - The **third expression** runs after each instance of the loop: We add one to the value of `i`. ``` #!/bin/bash for (( i=0; i<=5; i++ )) do echo "Iteration $1" done ``` The output from this script: ``` Iteration 0 Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 ``` Now you must be wondering **do we have any use case for such of “for loop” in real time scenario**. The answer would be **YES**, very much. I will share some real time scenarios to help you understand better: - In this example I will search for all files with the syntax matching `file*` under `/tmp` - Then append some content to these files - Now the loop should iterate based on the number of files found - Since the iteration starts from **0** we have given variable `i` to be one less than the number of files which is found ``` #!/bin/bash file_path=(`find /tmp -name "file*" -type f`) echo "Found ${#file_path[@]} files" for (( i=1; i<${#file_path[@]}; i++ )) do echo "Adding content to ${file_path[$i]}" echo "add some content" >> "${file_path[$i]}" done ``` The output from this script: ![Beginners Tutorial to use Bash For Loop](https://www.golinuxcloud.com/bash-for-loop/for_loop_2.webp) *bash for loop example* ### Define multiple variables We can also use **multiple variables** in single for loop. In this example I have defined *two variables* where the loop will iterate until `i` is less than equal to `j` variable ``` #!/bin/bash for (( i=1,j=5; i<=j; i++,j-- )) do echo "Iteration for i=$i" echo "Iteration for j=$j" done ``` The output from this script ``` Iteration for i=1 Iteration for j=5 Iteration for i=2 Iteration for j=4 Iteration for i=3 Iteration for j=3 ``` In this script I will perform some more operations using **two variables** in single for loop with bash ``` for (( i=0, j=0 ; i+j < 5 ; i++, j++ )) do echo "Value for i=$i" echo "Value for j=$j" echo "Multiply: $((i*j))" echo "add: $((i+j))" done ``` The output from this script ``` Value for i=0 Value for j=0 Multiply: 0 add: 0 Value for i=1 Value for j=1 Multiply: 1 add: 2 Value for i=2 Value for j=2 Multiply: 4 add: 4 ``` *** ## EX\_5: Use continue statement Now it is not mandatory that you would want to perform task for complete iteration, it is possible you may have a specific requirement to **ignore certain value** during the iteration run I have this sample script where the loop iterates for 5 times ``` for (( i=0 ; i<=5 ; i++ )) do if [ $i -eq 2 ];then echo "Don't do anything when i=2" fi echo "Doing something when i=$i" done ``` Now I have an additional check when value of the variable `i` is equal to `2`, so when this matches the script should do nothing and continue with the remaining iteration But let us check the output from this script ``` Doing something when i=0 Doing something when i=1 Don't do anything when i=2 Doing something when i=2 Doing something when i=3 Doing something when i=4 Doing something when i=5 ``` Even when I added a check to ignore `i=2` match, still we see “`Doing something when i=2`”, so **how to fix this?** We need to use `continue` statement for such case **The `continue` statement will exit the current iteration of the loop and will continue with the next iteration in the loop** ``` for (( i=0 ; i<=5 ; i++ )) do if [ $i -eq 2 ];then echo "Don't do anything when i=2" continue fi echo "Doing something when i=$i" done ``` The output from this script ``` Doing something when i=0 Doing something when i=1 Don't do anything when i=2 Doing something when i=3 Doing something when i=4 Doing something when i=5 ``` Now we see that when variable of `i` is equal to `2`, the iteration is skipped and the loop continues with remaining iteration *** ## EX\_6: Use break statement With `continue` we were able to skip the loop for single iteration but what if we want to **completely come out of the loop with a certain condition?** In this script we want to end the loop if the value of `i` variable is equal to `2` so with the if condition I have added a `break` statement ``` for (( i=0 ; i<=5 ; i++ )) do if [ $i -eq 2 ];then echo "Exiting, match sucessful" break fi echo "Doing something when i=$i" done ``` The output from this script ``` Doing something when i=0 Doing something when i=1 Exiting, match successful ``` As expected, our for loop terminates when the if condition is fulfilled using the `break` statement. *** ## EX\_7: One liner for loop examples We can write for loop in one liner commands to perform simple tasks using below **syntax**: ``` for {ELEMENT} in ${ARRAY[@]}; do [COMMAND_1]; [COMMAND_2]; [COMMAND_3]; done ``` In this shell script I will list all the network devices available on my server. here instead of defining an `ARRAY`, I have provided a `PATH` with `*`, so all the files and directories under that `PATH` would be considered as `ELEMENT` of the `ARRAY` ``` # for int in `ls /proc/sys/net/ipv4/conf/`;do echo "Interface: $int"; done Interface: all Interface: bond0 Interface: bond1 Interface: default Interface: eth0 Interface: eth1 Interface: eth2 Interface: eth3 Interface: lo ``` In this script I will remove all files under `/tmp` matching regex `file*` ``` # for file in /tmp/file*; do rm -vf $file; done removed '/tmp/file1' removed '/tmp/file2' removed '/tmp/file3' ``` *** ## Conclusion Even though bash is loosing it’s popularity with new programming language coming every day but engineers and developers still prefer bash to automate day to day tasks which consists of system commands. In such case it is always handy for engineers to be aware of using such loop based iteration. I have given some very basic examples for beginners to understand the syntax but this can be used to do complex tasks as well. Lastly I hope this tutorial to learn bash for loop with examples to iterate over a range and series of items on Linux and Unix was helpful. So, let me know your suggestions and feedback using the comment section. ![Deepak Prasad](https://www.golinuxcloud.com/images/authors/admin.jpg) Deepak Prasad R\&D Engineer Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects. [View all posts →](https://www.golinuxcloud.com/author/admin/) ## Related Articles [![Tips and Tricks to use Unshift in Bash with Examples](https://www.golinuxcloud.com/unshift-in-bash/unshift-in-bash_hu_bcc7244413f0dbc0.webp)Tips and Tricks to use Unshift in Bash with Examples](https://www.golinuxcloud.com/unshift-in-bash/) [![Create Bash Progress Bar like a PRO: Don't be a Rookie\!](https://www.golinuxcloud.com/create-bash-progress-bar/bash-progress-bar_hu_a872368accda002d.webp)Create Bash Progress Bar like a PRO: Don't be a Rookie\!](https://www.golinuxcloud.com/create-bash-progress-bar/) [![Create users in bulk using shell script \[SOLVED\]](https://www.golinuxcloud.com/create-users-in-bulk-using-shell-script/create-users-in-bulk_hu_f4f465b33f33bbad.webp)Create users in bulk using shell script \[SOLVED\]](https://www.golinuxcloud.com/create-users-in-bulk-using-shell-script/) [![Using Counter in Bash with Practical Examples](https://www.golinuxcloud.com/counter-in-bash/bash-counter_hu_4869d96619f65bf9.webp)Using Counter in Bash with Practical Examples](https://www.golinuxcloud.com/counter-in-bash/) [![Bash while loop to run command for specific time with examples](https://www.golinuxcloud.com/run-while-loop-until-specific-time-shell-bash/golinuxcloud-default_hu_f6c9f1c4d50174f9.webp)Bash while loop to run command for specific time with examples](https://www.golinuxcloud.com/run-while-loop-until-specific-time-shell-bash/) [![Create cron job or schedule jobs using bash scripts in Linux or Unix](https://www.golinuxcloud.com/create-schedule-cron-job-shell-script-linux/golinuxcloud-default_hu_f6c9f1c4d50174f9.webp)Create cron job or schedule jobs using bash scripts in Linux or Unix](https://www.golinuxcloud.com/create-schedule-cron-job-shell-script-linux/) [![Bash Trap Signals \| Capture CTRL+C \[Practical Examples\]](https://www.golinuxcloud.com/capture-ctrl-c-in-bash/bash_capture_ctrl_c_hu_89fa26aef2cffd47.webp)Bash Trap Signals \| Capture CTRL+C \[Practical Examples\]](https://www.golinuxcloud.com/capture-ctrl-c-in-bash/) [![Bash until vs while loop: Basic difference explained\!](https://www.golinuxcloud.com/bash-until-vs-while-loop/bash_until_vs_while_hu_d8e9bff9051c9c1d.webp)Bash until vs while loop: Basic difference explained\!](https://www.golinuxcloud.com/bash-until-vs-while-loop/) × ## Search GoLinuxCloud Showing results for: ![GoLinuxCloud](https://www.golinuxcloud.com/images/golinuxcloud-logo.webp) We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more. #### Interview Questions - [50 Maven Interview Questions and Answers for freshers and experienced](https://www.golinuxcloud.com/top-maven-interview-questions-answers-experienced/) - [20+ AWS Interview Questions and Answers for freshers and experienced](https://www.golinuxcloud.com/amazon-aws-interview-questions-answers-experienced/) - [100+ GIT Interview Questions and Answers for developers](https://www.golinuxcloud.com/git-interview-questions-answers-developers-linux/) - [100+ Java Interview Questions and Answers for Freshers & Experienced-2](https://www.golinuxcloud.com/java-interview-questions-answers-experienced-2/) - [100+ Java Interview Questions and Answers for Freshers & Experienced-1](https://www.golinuxcloud.com/java-interview-questions-answers-experienced/) - [Shell scripting Interview questions with answers](https://www.golinuxcloud.com/shell-scripting-interview-questions-with-answers/) #### Popular Posts - [How to stop ICMP ping flood attack (DOS) on Linux](https://www.golinuxcloud.com/prevent-icmp-ping-flood-attack-linux/) - [CPU, processors, core, threads - Explained in layman's terms](https://www.golinuxcloud.com/processors-cpu-core-threads-explained/) - [7 tools to detect Memory Leaks with Examples](https://www.golinuxcloud.com/how-to-find-memory-leaks/) - [100+ Linux Commands Cheat Sheet (With Practical Examples)](https://www.golinuxcloud.com/linux-commands-cheat-sheet/) - [Tutorial: Beginners guide on Linux Memory Management](https://www.golinuxcloud.com/tutorial-linux-memory-management-overview/) - [Top 15 tools to monitor disk IO performance with examples](https://www.golinuxcloud.com/monitor-disk-io-performance-statistics-linux/) - [Overview on different disk types and disk interface types](https://www.golinuxcloud.com/different-disk-types-interface-types-linux/) #### Company - [About](https://www.golinuxcloud.com/about-us/) - [Contact](https://www.golinuxcloud.com/contact-us/) - [Privacy](https://www.golinuxcloud.com/privacy-policy/) - [Disclaimer](https://www.golinuxcloud.com/disclaimer/) - [Buy Me a Coffee](https://buymeacoffee.com/golinuxcloud) © 2026 \| Hosted on [Rocket.net](https://rocket.net/?ref=deepakprasad)
Readable Markdown
I get this question a lot from many users, how can I run a certain command in a loop for a defined period of time? Or how can we iterate a task over a range only for n number of times. All these can be achieved using bash for loop in Linux and Unix From [What is Loop](https://whatis.techtarget.com/definition/loop), **a loop is a sequence of instructions that is continually repeated until a certain condition is reached**. So we know that a loop is a situation where we can perform a certain task repeatedly for a certain pre-defined period of time or may be some times infinite. Using such loops in our day to day task are very useful to automate stuffs using scripts. The loop can be configured using for, while, until etc depending upon individual’s requirement. In this tutorial we will understand in detail about bash for loop, and it’s usage across Linux environment for different types of automation shell scripts. I will also share different shell script examples using for loop to help you understand it’s usage. *** ## Basic for loop syntax in Bash The syntax of for loop would vary based on the programming language you choose such as C, perl, python, go etc. The provided **syntax** can be used only with bash and shell scripts ``` for {ELEMENT} in ${ARRAY[@]} do {COMMAND} done ``` ### Understanding the syntax - Here the `ARRAY` can contain any type of element such as strings, integers. - Although it is important that **you pass an `ARRAY`** to the loop, as if you pass a `VARIABLE` then the loop with count all the `ELEMENTS` from the `VARIABLE` as single `ELEMENT`. *I will show you what this means later in this article* - Loop will iterate each `ELEMENT` from the `ARRAY` and a certain command can be assigned to these `ELEMENT` - The loop will run until the last `ELEMENT` from `ARRAY` *** ## EX\_1: Loop over a range of numbers In our first shell script we will iterate over a range of numbers and print the number ``` for num in 1 2 3 4 5 6 do echo "Found Element: $num" done ``` Here we have passed a series of numbers separated by white space The output from this script ``` Found Element: 1 Found Element: 2 Found Element: 3 Found Element: 4 Found Element: 5 Found Element: 6 ``` *** ## EX\_2: Loop over a series of strings In this shell script we will iterate over a series of strings ``` for string in STRING1 STRING2 STRING3 do echo "Found Element: $string" done ``` The output from this script: ``` Found Element: STRING1 Found Element: STRING2 Found Element: STRING3 ``` Let us also take a [practical example for Linux Administrator](https://www.golinuxcloud.com/linux-commands-cheat-sheet/). In this script we will write a small script which will search for `file1`, `file2` and `file3` under `/tmp` Now if you are not using for loop then you have to manually search for these files. **You may think, so what it is easier to find files manually?** Yes, **TRUE** but what if you have 100 files or 1000 files? Would you find them manually? ``` for file in file1 file2 file3 do echo "Searching for file: $file" file_path=`find /tmp -name $file` if [ ! -z $file_path ];then echo "Found: $file at $file_path" else echo "Not Found: $file" fi done ``` The output from the script will tell us the files which were found and the files which were not found. ![Beginners Tutorial to use Bash For Loop](https://www.golinuxcloud.com/bash-for-loop/for_loop.webp) *Bash for loop in a range* *** ## EX\_3: Use for loop with an array Before we go ahead it is important that you understand the different between ARRAY and Variable ### Array vs Variable - It is **important** that when you use loop, you use an `ARRAY` **which contains elements separated by white character** - A variable will always contain a single element while an array can contain multiple number of elements - An array can also store multiple variables - You can [convert your VARIABLE into an ARRAY using different methods](https://www.golinuxcloud.com/bash-split-string-into-array-linux/ "Bash split string into array using 4 simple methods"). *For example:* ``` VAR="My name is Deepak" ``` Here we have given white space separated values, so does this become an `ARRAY`? **NO, this is a VARIABLE** Check the length of this `VARIABLE` ``` echo "Length: ${#VAR[@]}" Length: 1 ``` Bash could only count single element here, so if we try to iterate using this `VAR` then we get only `1` iteration You should know **how to declare an array in shell script**: Now I will declare the same content as an array ``` VAR=(My name is Deepak) ``` Here I have added the elements inside *parenthesis*, now let us check the length of this variable ``` # echo "Length: ${#VAR[@]}" Length: 4 ``` So now bash considers this as an `ARRAY` with **4** `ELEMENTS` ``` 1st - My 2nd - name 3rd - is 4th - Deepak ``` We can **access these individual elements** using `${VAR[$num]}` Here `$num` should be replaced by the element number you want to access, *for example*: ``` #!/bin/bash VAR=(My name is Deepak) echo "First Element: ${VAR[0]}" echo "First Element: ${VAR[1]}" echo "First Element: ${VAR[2]}" echo "First Element: ${VAR[3]}" ``` The output of this script: ![Beginners Tutorial to use Bash For Loop](https://www.golinuxcloud.com/bash-for-loop/for_loop_1.webp) *Bash for loop array* Let us take a **practical example** to understand loop with array in real time environment. Assume you have to *create 5 users* and *assign a password to these users*. In this shell script I will assign “`Passw0rd`” as password to all the users ``` #!/bin/bash USERS=(user1 user2 user3 user4 user5) for user in ${USERS[@]} do echo "Creating user $user" useradd $user echo "Assigning password for $user" echo "Passw0rd" | passwd "$user" --stdin done ``` *** ## EX\_4: Using for loops in bash as C programmers - Bash extends the `for` keyword to provide functionality similar to the three-argument **for loop used in C using double parenthesis** - We can use the above examples when we have a small number of range to iterate over but what if we have a *larger range of items*? - Or if we do not know the number of times the loop has to run and instead we get this value internally from the script? **Syntax:** ``` for ((ASSIGN_VALUE; ASSIGN_LIMIT ; STEP_UP)) do [COMMANDS] done ``` - The **syntax** would be more clear in this sample script. - The **first expression** is run before the loop starts: We assign the `i` value to zero to start it off. - The **second expression** is the test used to determine whether the loop should continue or stop: We test whether the value of `i` is less than 5. - The **third expression** runs after each instance of the loop: We add one to the value of `i`. ``` #!/bin/bash for (( i=0; i<=5; i++ )) do echo "Iteration $1" done ``` The output from this script: ``` Iteration 0 Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 ``` Now you must be wondering **do we have any use case for such of “for loop” in real time scenario**. The answer would be **YES**, very much. I will share some real time scenarios to help you understand better: - In this example I will search for all files with the syntax matching `file*` under `/tmp` - Then append some content to these files - Now the loop should iterate based on the number of files found - Since the iteration starts from **0** we have given variable `i` to be one less than the number of files which is found ``` #!/bin/bash file_path=(`find /tmp -name "file*" -type f`) echo "Found ${#file_path[@]} files" for (( i=1; i<${#file_path[@]}; i++ )) do echo "Adding content to ${file_path[$i]}" echo "add some content" >> "${file_path[$i]}" done ``` The output from this script: ![Beginners Tutorial to use Bash For Loop](https://www.golinuxcloud.com/bash-for-loop/for_loop_2.webp) *bash for loop example* ### Define multiple variables We can also use **multiple variables** in single for loop. In this example I have defined *two variables* where the loop will iterate until `i` is less than equal to `j` variable ``` #!/bin/bash for (( i=1,j=5; i<=j; i++,j-- )) do echo "Iteration for i=$i" echo "Iteration for j=$j" done ``` The output from this script ``` Iteration for i=1 Iteration for j=5 Iteration for i=2 Iteration for j=4 Iteration for i=3 Iteration for j=3 ``` In this script I will perform some more operations using **two variables** in single for loop with bash ``` for (( i=0, j=0 ; i+j < 5 ; i++, j++ )) do echo "Value for i=$i" echo "Value for j=$j" echo "Multiply: $((i*j))" echo "add: $((i+j))" done ``` The output from this script ``` Value for i=0 Value for j=0 Multiply: 0 add: 0 Value for i=1 Value for j=1 Multiply: 1 add: 2 Value for i=2 Value for j=2 Multiply: 4 add: 4 ``` *** ## EX\_5: Use continue statement Now it is not mandatory that you would want to perform task for complete iteration, it is possible you may have a specific requirement to **ignore certain value** during the iteration run I have this sample script where the loop iterates for 5 times ``` for (( i=0 ; i<=5 ; i++ )) do if [ $i -eq 2 ];then echo "Don't do anything when i=2" fi echo "Doing something when i=$i" done ``` Now I have an additional check when value of the variable `i` is equal to `2`, so when this matches the script should do nothing and continue with the remaining iteration But let us check the output from this script ``` Doing something when i=0 Doing something when i=1 Don't do anything when i=2 Doing something when i=2 Doing something when i=3 Doing something when i=4 Doing something when i=5 ``` Even when I added a check to ignore `i=2` match, still we see “`Doing something when i=2`”, so **how to fix this?** We need to use `continue` statement for such case **The `continue` statement will exit the current iteration of the loop and will continue with the next iteration in the loop** ``` for (( i=0 ; i<=5 ; i++ )) do if [ $i -eq 2 ];then echo "Don't do anything when i=2" continue fi echo "Doing something when i=$i" done ``` The output from this script ``` Doing something when i=0 Doing something when i=1 Don't do anything when i=2 Doing something when i=3 Doing something when i=4 Doing something when i=5 ``` Now we see that when variable of `i` is equal to `2`, the iteration is skipped and the loop continues with remaining iteration *** ## EX\_6: Use break statement With `continue` we were able to skip the loop for single iteration but what if we want to **completely come out of the loop with a certain condition?** In this script we want to end the loop if the value of `i` variable is equal to `2` so with the if condition I have added a `break` statement ``` for (( i=0 ; i<=5 ; i++ )) do if [ $i -eq 2 ];then echo "Exiting, match sucessful" break fi echo "Doing something when i=$i" done ``` The output from this script ``` Doing something when i=0 Doing something when i=1 Exiting, match successful ``` As expected, our for loop terminates when the if condition is fulfilled using the `break` statement. *** ## EX\_7: One liner for loop examples We can write for loop in one liner commands to perform simple tasks using below **syntax**: ``` for {ELEMENT} in ${ARRAY[@]}; do [COMMAND_1]; [COMMAND_2]; [COMMAND_3]; done ``` In this shell script I will list all the network devices available on my server. here instead of defining an `ARRAY`, I have provided a `PATH` with `*`, so all the files and directories under that `PATH` would be considered as `ELEMENT` of the `ARRAY` ``` # for int in `ls /proc/sys/net/ipv4/conf/`;do echo "Interface: $int"; done Interface: all Interface: bond0 Interface: bond1 Interface: default Interface: eth0 Interface: eth1 Interface: eth2 Interface: eth3 Interface: lo ``` In this script I will remove all files under `/tmp` matching regex `file*` ``` # for file in /tmp/file*; do rm -vf $file; done removed '/tmp/file1' removed '/tmp/file2' removed '/tmp/file3' ``` *** ## Conclusion Even though bash is loosing it’s popularity with new programming language coming every day but engineers and developers still prefer bash to automate day to day tasks which consists of system commands. In such case it is always handy for engineers to be aware of using such loop based iteration. I have given some very basic examples for beginners to understand the syntax but this can be used to do complex tasks as well. Lastly I hope this tutorial to learn bash for loop with examples to iterate over a range and series of items on Linux and Unix was helpful. So, let me know your suggestions and feedback using the comment section.
Shard143 (laksa)
Root Hash4697542821095851143
Unparsed URLcom,golinuxcloud!www,/bash-for-loop/ s443