Software & Apps Linux 151 151 people found this article helpful How to Write a BASH 'for' Loop How to use the BASH 'for' loop in shell scripts by Juergen Haas Writer Former Lifewire writer Juergen Haas is a software developer, data scientist, and a fan of the Linux operating system. our editorial process Juergen Haas Updated on February 04, 2021 reviewed by Chris Selph Lifewire Tech Review Board Member Chris Selph is a CompTIA-certified technology and vocational IT teacher. He also serves as network & server administrator and performs computer maintenance and repair for numerous clients. our review board Article reviewed on Jul 10, 2020 Chris Selph Tweet Share Email Linux Switching from Windows What to Know In a Bash for loop, all statements between do and done are performed once for every item in a list or number range.With a big list, use in {list} to loop between a start and end point. Use ellipsis to iterate a full number range, e.g., for number in {1..10}.To skip certain numbers, add a third number to the range. For example, use {0..100..10} to only list every 10th number. In a programming or scripting language, Bash offers several ways to repeat code—a process called looping—where a for loop repeats a certain section of the code. This allows a series of commands to run until a particular condition is met. In this guide, we show you how to write a Bash for Loop. How to Loop Through a List Scripting languages such as Bash feature similar programming constructs as other languages. For example, import parameters get input from the keyboard and store these inputs as variables, which then perform a certain action based on the value of the input parameters. Consider a simple example script titled loop.sh: #!/bin/bashfor number in 1 2 3 4 5doecho $numberdoneexit 0 The Bash way of using for loops is somewhat different from the way other programming and scripting languages handle for loops. Let's break the script down. In a BASH for loop, all the statements between do and done are performed once for every item in the list. In this example, the list is everything that comes after the word in—the numbers 1 2 3 4 5. Each time the loop iterates, the next value in the list is inserted into the variable specified after the word for. In the above loop, the variable is called number. The echo statement displays information to the screen. Therefore, this example takes the numbers 1 through 5 and outputs each number one by one to the screen: How to Loop Between a Start and End Point The trouble with this loop.sh script is that if you want to process a bigger list (for example, 1 to 500), it would take ages to type all the numbers. Instead, specify a start and endpoint: #!/bin/bashfor number in {1..10}doecho "$number "doneexit 0 The rules are the same. The values after the word in make up the list to iterate through, and each value in the list is placed in the variable (that is, number), and each time the loop iterates, the statements between do and done are performed. The main difference is the way the list is formed. The curly brackets denote a range, and the range, in this case, is 1 to 10 (the two dots separate the start and end of a range). This example, therefore, runs through each number between 1 and 10 and outputs the number to the screen. The same loop could have been written like this, with syntax identical to the first example: for number in 1 2 3 4 5 6 7 8 9 10 How to Skip Numbers in a Range The previous example showed how to loop between a start and endpoint. Here's how to skip numbers in the range. For example, to loop between 0 and 100 but only show every tenth number, use the following script to obtain this output: #!/bin/bashfor number in {0..100..10}doecho "$number "doneexit 0 The rules are the same. There is a list, a variable, and a set of statements to be performed between do and done. The list this time looks like this: {0..100..10}. The first number is 0 and the end number is 100. The third number (10) is the number of items in the list that it will skip. The above example, therefore, displays the following output: A Practical Example For loops do more than iterate lists of numbers. For example, use the output of other commands as the list. The following example shows how to convert audio files from MP3 to WAV: #!/bin/bashfor file in ./*.mp3dompg -w ./wavs/"${file}".wav "$file"done The list in this example is every file with the .MP3 extension in the current folder, and the variable is a file. The mpg command converts the MP3 file into WAV. However, you probably need to install this tool using a package manager first. Was this page helpful? Thanks for letting us know! Get the Latest Tech News Delivered Every Day Email Address Sign up There was an error. Please try again. You're in! Thanks for signing up. There was an error. Please try again. Thank you for signing up. Tell us why! Other Not enough details Hard to understand Submit