Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.

Using multiple variables in a for loop
I want to use two variables in a for loop like this (This is for example, I'm not going to execute seq like operations)
Expected output is
1 www.google.com 2 www.yahoo.com
- command-line

3 Answers 3
If i was to just be a number that incremented with each string, you could try a for loop and increment i with each iteration.
For example:

- why did you, use double braces in $((i++)) . I think one braces is enought for this. Please correct me , If i said wrong. – SuperKrish Nov 5, 2016 at 8:32
- 1 @SuperKrish $(( . . .)) is arithmetic expansion, which is what makes i++ work. With single braces that's $(. . .) command substitution , so i++ won't work there. If you were trying to catch output of another program, say $( df ) for example, you could use single braces. If you need calculation, then you use $(( . . . )) structure – Sergiy Kolodyazhnyy Nov 5, 2016 at 10:30
- Great! @jake My doubt is clear – SuperKrish Nov 5, 2016 at 10:47
Lets create variable to point to the file location
FILE="/home/user/myfile"
The file content:
To get the output of:
It can be done by one of the following methods below:
Using counter variable:
Using cat -n (number all output lines)
Using array:
If your file already with line numbers, example:
Loop and split every line to array:
For more info:
How to increment a variable in bash?
Loop Through Array of Strings in Bash Script
Arrays Syntax

Here are some alternative ways, two short and simple ones:
which both output:
and the slightly more complex:
that outputs:
In that second suggestion, I'm using an array named s created with the line s=(xx yy)
The ${#s[@]} syntax is the number of elements in the array, here 2 and the ${s[i-1]} is element at offset i-1 from the beginning of the array, thus ${s[1-1]} is ${s[0]} and then is www.google.com , etc.

- I dont know what is means ${#s[@]} `${s[i-1]}1. Here s is the variable loaded with some values.Can you clear my doubt? – SuperKrish Nov 5, 2016 at 14:15
- Answer edited with explanations added. – jlliagre Nov 5, 2016 at 15:26
You must log in to answer this question.
Not the answer you're looking for browse other questions tagged command-line bash ..
- The Overflow Blog
- Journey to the cloud part I: Migrating Stack Overflow Teams to Azure
- Featured on Meta
- Our Design Vision for Stack Overflow and the Stack Exchange network
- AI-generated content is not permitted on Ask Ubuntu
Hot Network Questions
- Why do some US senators such as Mitch McConnell, Dianne Feinstein etc last for so long in the senate?
- Would it harm game balance to allow potions to be self-administered with a bonus action?
- How likely is it that the Voyager spacecrafts haven't yet been damaged by micrometeoroids?
- Silicone food molds that have been used to prepare (caustic soap): Safe for use with food again?
- Male and female seahorses: which is which
- Idiom for frustrating someone else's plans by taking what the other person wanted in the first place
- Why is the convolution of two sine waves a sinc function?
- Why does Canada's superficial loss rule also include a clause for "30 days before the sale"?
- Single letter abbrev and expansion case
- Are there any good alternatives to firearms for 1920s aircrafts?
- Electrical issues while central air is running
- Does theism have the burden of proof?
- Is there any way I can recycle a device that has been iCloud locked?
- Why can't I move my knight from d5 to c7?
- What is the difference between computation and simulation?
- Why are hangars so high?
- The caption is not spanning the entire width of the table
- Typical pitch interval of the exclamation "uh-oh!"
- Is this duplo train track under too much tension?
- Proper way to combine same-frequency PCB antennas
- Be big more often
- Time to get rid of Rust-Oleum Textured Stone spray paint finish on wood
- Are multiple extension cords in series causing significant increase in electric use?
- Does query string case affect SEO?
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .
Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
How to pass multiple variable to a for loop as a argument in a bash script? [closed]
I am new to linux and i am writing a bash script... In the script, I have 2 variables(inside the varibles contents are there). I am trying to pass these 2 varibales in a same for loop and perform some actions.
But when i pass 2 varibles in a same for loop, am getting error. In the below code. to make it ease I am passing two paramerters but in actual it will vary. I am getting output of these varibales from a command
Below is my code:
Could any one pls help me to pass 2 arguments in the for loop at the same time.
Argument should pass like in the for loop
Below is the error message

Kindly help!
Below is my output

Below is my curl coomand
- Please post the error as well – cutrightjm May 4, 2020 at 5:33
- @cutrightjm Syntax error in for loop – clarie May 4, 2020 at 5:36
- It is helpful when you ask the question to include the running of the script and the error. – cutrightjm May 4, 2020 at 5:40
- 1 you're missing a do after the first for – pLumo May 4, 2020 at 6:01
- 1 Please edit your question to explain what you want to achieve as well how you think you should do it. Also show where your input data comes from and provide an example of it. There are already two possible answers that have been invalidated because you didn't give us the necessary detail in the question . For example, in your question you're reading data from files but in response to one answer you say the data is in environment variables. There are people who want to help you so make it easy for them to give you a plausible answer. – roaima May 4, 2020 at 6:52
4 Answers 4
Here's how I'd write it:
It reads the two variables from the heredoc that is supplied at the end.

- In my case I have 100 parametrs and it will vary. Its not possible to give the variables at the end. – clarie May 4, 2020 at 6:14
- @clarie Beginning, end… What's the difference? Do you mean the values cannot be hardcoded in the script or what? Are they passed via already existing environment variables? What condition are you not telling us? Robin's code works for me. – Kamil Maciorowski May 4, 2020 at 6:25
- @KamilMaciorowski yes, variables are passed in the existing environment variables. We can't hardcode it – clarie May 4, 2020 at 6:36
- @KamilMaciorowski I have editied my question – clarie May 4, 2020 at 6:49
The percent sign question mark %? chops off the / that you didn't seem to want.
Put that in a file named myscript.sh then run it:
Update: Output with printf replaced by echo curl ... :
- I tried the above code...As expected loop are executing twice.But Both times sames valus are coming in. 2019_06/ 100 – clarie May 4, 2020 at 6:48
- I edited my answer to show the output I get. It is not the same values repeated. – Robin A. Meade May 4, 2020 at 6:51
- yes when i run as updtaed, in the terminal values are not repaeted. But when i use curl to update in the influxdb.only first value is getting inserted. – clarie May 4, 2020 at 6:59
- I suggest you echo the curl invocation to the terminal to check it. Put echo before curl . Once you are sure the curl command looks right, remove the echo at the beginning of that line to execute curl for real. You might need to post a separate question about your curl invocation. – Robin A. Meade May 4, 2020 at 7:05
- I used echo and i also posted the output in the question sction. – clarie May 4, 2020 at 7:14
Putting multiple values in one variable, using it unquoted and relying on word splitting to separate the values again is not a good practice. Use arrays. You can retrieve values from two or more arrays "in parallel" by using the same index; this should solve your problem.
Prepare two arrays of equal size and loop like this:
Replace echo … with the desired (and adjusted) if … curl … block.
If FILES and FILESIZE are variables through which your script receives data and you cannot change this and you must rely on word splitting, then create arrays from the variables:
The variables are not quoted, word splitting (and filename generation ) occurs at this stage.
- In this case..Size is not fixed..It will vary... I think its not feasible to decleae one by one.. Could u pls help me – clarie May 4, 2020 at 6:10
- @clarie What do you mean? The loop depends on the length of the file array ( ${#file[@]} ), so it will work for longer arrays as well. – Kamil Maciorowski May 4, 2020 at 6:16
- Yes both file and filesize will have the same length. and the loop should go through till the end – clarie May 4, 2020 at 6:16
- @clarie the loop should go through till the end – And it does. Have you tried my code with longer arrays? – Kamil Maciorowski May 4, 2020 at 6:28
- @clarie See the second part of my answer. – Kamil Maciorowski May 4, 2020 at 6:53
Often you will want todo S3 stuff in parallel:

Not the answer you're looking for? Browse other questions tagged linux bash shell for .
- The Overflow Blog
- Journey to the cloud part I: Migrating Stack Overflow Teams to Azure
- Featured on Meta
- Our Design Vision for Stack Overflow and the Stack Exchange network
Hot Network Questions
- Shareware computer game where you play a starfighter pilot
- Are multiple extension cords in series causing significant increase in electric use?
- Male and female seahorses: which is which
- Why can we take the collection of all open balls?
- CAT: Cat Approved TUIs
- Least Count Error is Random or Systematic?
- Does theism have the burden of proof?
- show that solutions which start in the first quadrant must remain there for all time
- How time flies or flew?
- Electrical issues while central air is running
- Does query string case affect SEO?
- How does Legendre transformation in classical mechanics relate to Adrien-Marie Legendre?
- In how many ways can four distinctive letters be posted in 6 post boxes such that any two go in same post box and remaining go to different boxes?
- How many counts does a mouse need to report to move the windows cursor for 1 pixel exactly?
- Why are hangars so high?
- Are there any good alternatives to firearms for 1920s aircrafts?
- anova degrees of freedom
- Graphic3D, white face, black edge in any view?
- I’m feeling overwhelmed about writing a novel, can anyone help?
- What should the table do when using multicolumn multiple times?
- Are these lines by Thomas Nuce a translation and/or taken from a longer poem?
- Why is the convolution of two sine waves a sinc function?
- What are some good books on mathematical pedagogy?
- Why do STM32 MCUs divide RAM into SRAM1 and SRAM2?
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .
How-To Geek
9 examples of for loops in linux bash scripts.
How to repeat yourself, elegantly.
Quick Links
The for loop, simple for loops, c-like for loops, for loops using word arrays, for loops using associative arrays, iterating over the output of commands, the dazzling for loop.
The versatile Bash for loop does much more than loop around a set number of times. We describe its many variants so you can use them successfully in your own Linux scripts.
All scripting and programming languages have some way of handling loops. A loop is a section of code that you want to have executed repeatedly. Rather than type the same set of instructions into your script , again and again, a loop will repeat one section of code over and over for you.
Related: How to Use Bash If Statements (With 4 Examples)
The Bash for loop is very flexible. It can work with numbers, words, arrays, command line variables, or the output of other commands. These are used in the header of the loop. The header dictates what the loop is working with---numbers or strings, for example---and what the end condition is that will stop the looping.
The body of the loop contains the code that you want to have repeated. It holds what you want the loop to do. The loop body can contain any valid script command.
A variable called the loop counter or iterator is used to step through a range of values or a list of data items. For each loop, the iterator takes on the value of the next number, string, or whatever data type the loop is iterating over. This allows the loop to work with the values of each of the data items in turn, or even in some cases to manipulate the data items themselves.
If you're looking to write your first for loop, these simple examples will get you started.
for Loops using Numerical Lists
You can run a for loop on the command line. This command creates and executes a simple for loop. The iterator is a variable called i . We're going to assign i to be each of the values in the list of numbers, in turn. The body of the loop is going to print that value to the terminal window. The condition that ends this loop is when i has iterated across the entire list of numbers.
for i in 1 2 3 4 5; do echo $i; done
It's important to note here that the variable i is increased by one each time the loop spins round, but that's because the list of numbers goes up by one each time.
This list of numbers starts at 3 and goes up in steps of two, then arbitrarily leaps to 44.
for i in 3 5 7 9 11 44; do echo $i; done
It makes no difference to the for loop. It starts at one end of the list and uses each value in turn, until all the values in the list have been used.
Nor do the numbers need to be in ascending order. They can be in any order.
for i in 3 43 44 11 9; do echo $i; done
for Loops Using Word Lists
We can just as easily do the same with words. Copy the text of the script into an editor and save it as "word-list.sh."
#!/bin/bash
for word in This is a sequence of words
echo $word
You'll need to use chmod to make the script executable, and any other script you copy out of this article. Just substitute the name of the script each time you use the chmod command.
chmod +x word-list.sh
Let's run the script.
./word-list.sh
Just as it did with the numbers, the iterator---in this example, the variable word ---works its way through the list of data items until it reaches the end of the list. The loop body accesses the value in the word variable and so each word in the list gets processed.
for Loops with Number Ranges
If you wanted a for loop to run 100 times it would be a pretty tiresome affair to have to type in a sequence of 100 numbers in the loop header. Number ranges let you specify the first and last number only.
This script is "number-range.sh."
for i in {1..10}
echo "Loop spin:" $i
The number range is defined within curly brackets " {} " with two periods " .. " separating the numbers that start and end the range. Make sure you don't include any whitespace in the range definition.
This is how it runs:
./number-range.sh
You can include another number that defines the step size the iterator should use to walk through the numbers in the range. This script, "number-range2.sh" will use a range of 0 to 32, and a step size of 4.
for i in {0..32..4}
The iterator steps through the number range in jumps of four.
./number-range2.sh
for Loops Using Filenames
Because we can process lists of words, we can get our scripts to work with filenames. This script is called "filenames.sh."
for file in word-list.sh number-range.sh number-range2.sh filenames.sh
ls -lh "$file"
It would be pretty pointless to have a script that only does what ls can do, but it does demonstrate how to access filenames inside the loop body.
./filenames.sh
In a similar way to using the number range, we can use a file pattern in the loop header to specify the files we want to process. This avoids a lot of typing and means we don't need to know in advance the names of the files.
This script is called "filenames2.sh." We've replaced the list of filenames with the filename pattern "*.sh" to have the script report on all script files in the current directory.
for file in *.sh
Here's the output.
./filenames2.sh
for Loops Using Command Line Parameters
We can add some more flexibility by passing in the filename pattern on the command line. The $* variable represents all of the command line parameters passed to the script.
This is "filenames3.sh."
for file in $*
We'll ask for filenames that begin with "n" and have an SH extension.
./filenames3.sh n*.sh
We can also pass in more than one pattern at a time.
./filenames3.sh n*.sh .bashrc
The iterator variable file takes on the value of each of the command line parameters. Filename patterns are expanded, and all of the filenames are processed in the loop body.
Related: How to Work with Variables in Bash
Bash supports the classic three-term for loop, such as those found in the C programming language. They're called three-term for loops because there are three terms in the loop header.
- The initial value of the loop iterator.
- The test for whether the loop continues or ends.
- The incrementing---or decrementing---of the iterator.
This script is "c-like.sh."
The iterator I is set to 1 at the start of the loop, and the loop will run for as long as the statement " i<=10 " is true. As soon as i reaches 11, the for loop will stop. The iterator is being increased by one, every revolution of the loop.
for (( i=1; i<=10; i++ ))
echo "Loop number:" $i
Let's run this script.
./c-like.sh
The C-like for loop permits the easy creation of for loops that have slightly odd requirements. This loop starts at 15, and counts backward in steps of 3. This is "c-like2.sh"
for (( i=15; i>0; i-=3 ))
When we run it, it should jump backward in steps of three.
./c-like2.sh
Infinite for Loops
You can also use this format of for loop to create an infinite loop. All you need do is remove all of the elements from the loop header, like this. This is "infinite.sh."
for (( ; ; ))
echo "Press Ctrl+C to stop..."
You'll need to hit Ctrl+C to stop the loop.
./infinite.sh
We can easily iterate through an array of words. We need to provide the name of the array in the loop header, and the iterator will walk through all entries in the array. This is "word-array.sh."
distributions=("Ubuntu Fedora Manjaro Arch EndeavourOS Garuda")
for distro in $distributions
echo $distro
All the distributions are listed for us.
./word-array.sh
The continue Command
If you want the loop to step over a particular entry, test whether the iterator matches that entry and use the continue command. The continue command abandons the current spin of the loop. It increments the iterator and starts the next spin of the loop---assuming the entry you want to skip over isn't the last item in the list.
This is "word-array2.sh." It steps over the "Arch" array entry but processes all other array members.
if [[ "$distro" == "Arch" ]] ;
continue
"Arch" doesn't appear in the terminal window.
./word-array2.sh
The break Command
The break command breaks out of the loop and prevents any more processing.
This is "word-array3.sh." It's the same as the previous script with continue replaced by break .
When the iterator contains "Arch" the for loop abandons any more processing.
./word-array3.sh
In Bash 4 and higher, associative arrays allow you to create lists of key-value pairs that can be searched by the key or by the value. Because of the two-way relationship between the key and the value, they're also called data dictionaries.
Related: What Are Bash Dictionaries on Linux, and How Do You Use Them?
We can iterate through an associative array using a for loop. This script is "associative.sh." It defines an associative array with four entries in it, one for each of "dog", "cat", "robin" , and "human." These are the keys. The values are the (default) number of legs they each have.
declare -A animals=( [dog]=Four-legged [cat]=Four-legged [robin]=Two-legged [human]=Two-legged )
for legs in ${!animals[@]}
if [ ${animals[$legs]} == "Two-legged" ]; then
echo ${legs}
The iterator is called legs . Note that the loop header contains an " ! " exclamation point. This is not acting as the logical NOT operator, it's part of the associative array syntax. It is required to search through the array.
The body of the loop performs a string comparison test. If the value of the array member is "Two-legged", it prints the key value to the terminal window. When we run it, the script prints the two-legged creatures.
./associative.sh
If you have a command or sequence of commands that produce a list of something, such as filenames, you can iterate through them with a for loop. You need to watch out for unexpected filename expansions, but in simple cases it is fine.
This script is "command.sh." it uses ls and wc to provide a sorted list of script file names, together with their line, word, and byte counts.
for i in $(ls *.sh | sort);
echo $(wc $i)
When we run it we get the statistics for each file, with the files listed in alphabetical order.
./command.sh
The for loop is a versatile and easily understood scripting tool. But as flexible as it is, don't forget that other loops exist for a reason. Don't be dazzled into thinking the for loop is all you'll ever need.
Related: How to Process a File Line by Line in a Linux Bash Script
The while loop, for example, is much better suited for certain things than the for loop, such as reading lines from a file .
Writing good scripts means using the most suitable tool for the task at hand. The for loop is a great tool to have in your toolbox of tricks.
- Stack Overflow Public questions & answers
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Talent Build your employer brand
- Advertising Reach developers & technologists worldwide
- Labs The future of collective knowledge sharing
- About the company
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Bash: Two for loops at once?
I am not sure how to do this at all.
I have two text files, FILE1 and FILE2 .
I would like to run a for loop for each file at the same time and display the contents next to each other.
For example,
for $i in $(cat FILE1); do echo $i; done
for $j in $(cat FILE2); do echo $j; done
I would like to combine these two commands, so I can run both files at the same time and have an output like $i $j

- 3 That is precisely what paste does. – rici Aug 18, 2018 at 5:25
- 1 If you don't want paste (though I guess you do) maybe look at Looping over pairs of values in Bash – tripleee Aug 18, 2018 at 5:51

4 Answers 4
Use the paste command
paste FILE1 FILE2
Details for paste command
Another resource
You can do this if they have the same number of lines.
You can extend it to what you want for unequal number of lines.
- 3 Solution 2 is horribly inefficient, and should never be used. – chepner Aug 18, 2018 at 13:13
You shouldn't be using for loops at all; see Bash FAQ 001 . Instead, use two read commands in a single while loop.
Each read command reads from a separate file descriptor. In this version, the loop will exit when the shorter of the two files is exhausted.
There are two different questions being asked here. Other answers address the question of how to display the contents of the file in 2 columns. Running two loops simultaneously (which is the wrong way to address the first problem) can be done by running them each asynchronously: for i in ${seqi?}; do ${cmdi?}; done & for j in ${seqj?}; do ${cmdj?}; done & wait
Although you could also implement paste -d ' ' file1 file2 with something like:
- I just noticed that @chepner gave nearly this same solution, but I'll not delete mine since it addresses the issue of the files having different line lenghts. But this is totally a hack and should never be done. – William Pursell Aug 18, 2018 at 13:31
Another option, in bash v4+ is to read the two files into 2 arrays, then echo the array elements side-by-side:
Or change the echo to printf if you want the columns to line up:
I'm not suggesting you do this if your files are 100s of megabytes.

Your Answer
Sign up or log in, post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct .
Not the answer you're looking for? Browse other questions tagged linux bash shell ubuntu for-loop or ask your own question .
- The Overflow Blog
- Journey to the cloud part I: Migrating Stack Overflow Teams to Azure
- Featured on Meta
- Our Design Vision for Stack Overflow and the Stack Exchange network
- Temporary policy: Generative AI (e.g., ChatGPT) is banned
- Call for volunteer reviewers for an updated search experience: OverflowAI Search
- Discussions experiment launching on NLP Collective
Hot Network Questions
- Did D-Wave show quantum advantage in 2023?
- Loading LSB first or MSB first?
- Poison ivy chess: Can the king get to safety?
- anova degrees of freedom
- Be big more often
- Does query string case affect SEO?
- Why is there nearly no 1x road bikes?
- TikZ animations key `end on` is interpreted as `begin on`
- Are these lines by Thomas Nuce a translation and/or taken from a longer poem?
- Coworker keeps telling me where to sit. How can I handle this?
- Can one performer manage striking tubular bells and timpani simultaneously?
- Is "legalese" a thing in languages other than English?
- Why can we take the collection of all open balls?
- Least Count Error is Random or Systematic?
- I’m feeling overwhelmed about writing a novel, can anyone help?
- Why are all eigen solvers iterative?
- How to put a tube in a used tubeless tire
- Single letter abbrev and expansion case
- Why do STM32 MCUs divide RAM into SRAM1 and SRAM2?
- Is there any way I can recycle a device that has been iCloud locked?
- Why do people do a postdoc, rather than getting another PhD?
- Does anyone know this skyline?
- Calculating integral with exponential function and parameter
- show that solutions which start in the first quadrant must remain there for all time
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .
- Mobile Site
- Staff Directory
- Advertise with Ars
Filter by topic
- Biz & IT
- Gaming & Culture
Front page layout
for comment in $comments ; upvote $comment ; done —
Command line wizardry, part two: variables and loops in bash, learn to process thousands of items reliably and repeatably in this installment..
Jim Salter - Sep 20, 2021 11:15 am UTC

In our first tutorial on command line wizardry, we covered simple redirection and the basics of sed, awk, and grep. Today, we're going to introduce the concepts of simple variable substitution and loops—again, with a specific focus on the Bash command line itself, rather than Bash scripting.
If you need to write a script for repeated use—particularly one with significant logical branching and evaluation—I strongly recommend a "real language" instead of Bash. Luckily, there are plenty of options. I'm personally a big fan of Perl, in part because it's available on pretty much any *nix system you'll ever encounter. Others might reasonably choose, say, Python or Go instead, and I wouldn't judge.
The real point is that we're focusing on the command line itself. Everything below is something you can easily learn to think in and use in real time with a little practice.
Setting and getting values in Bash
Bash handles variables a bit oddly compared to any of the "real" languages I've used. In particular, you must refer to the same variable with different syntax when setting its value versus when retrieving it.
Let's take a look at a very simple example:
If you've never worked with variables in Bash before, you might think there's a typo in the above example. We set the value of hello but read back its value as $hello . That's not a mistake—you don't use the leading $ when defining/setting a variable, but you must use it when reading from the same variable later.
Destroying variables
If we need to clear that variable, we can use the unset command—but, again, we must refer to hello instead of $hello :
Thinking about the error messages we get when trying incorrectly to unset $hello should make the problem clear. When we unset $hello , we're passing the value of hello to unset rather than passing the variable name itself. That means exactly what you think it does:
Although the switch back and forth between referencing hello and $hello is deeply unsettling if you're only familiar with "real" languages, Bash is at least relatively consistent about when it uses each style. With a little practice, you'll get used to this—even if you never feel truly comfortable about it.
Variable scope
Before we move on from the basics of variables, we need to talk about variable scope. Basically, a Bash variable is limited in scope to only the current process. It won't pass your variables on to any child processes you initiate. We can see this in action most easily by calling bash from bash :
In our original Bash session, we set the variable hello to equal "Hello, world!" and access it successfully with the echo command. But when we start a new bash session, hello is not passed on to that child session. So when we again try to echo $hello , we get nothing. But after exiting the child shell, we can once again successfully echo $hello and receive the value we originally set.
If you need to carry variables set in one session into a child process, you need to use the export command:
As you can see, export successfully flagged our variable hello for passing down to child processes—in this case, another instance of bash . But the same technique works, in the same way, for calling any child process that references environment variables. We can see this in action by writing a very small Perl script:
Just like our earlier example of a child session of bash , our Perl script can't read our hello environment variable unless we first export it.
The last thing we'll say about export is that it can be used to set the value of the variable it's exporting, in one step:
Now that we understand how to set and read values of variables on the command line, let's move on to a very useful operator: $() .
Capturing command output with $()
The $() command substitution operator captures the output of a command. Sounds simple enough, right? In the above example, we demonstrate that the output of the command echo test is, of course, "test"—so when we echo $(echo test) we get the same result.
Backticks can provide the same result—eg, echo `echo test` —but $() is generally preferred, since unlike backticks, the $() operator can be nested.
We can take this a step further and instead assign the output of a command into a variable:
Finally, we can combine this with the lessons from our first tutorial and strip the first column from du 's output to assign to our variable:
At this point, we understand how variables and the $() operator work. So let's talk about simple looping structures.
For loops in Bash
The above example concisely demonstrates the way a simple for loop works in Bash. It iterates through a supplied sequence of items, assigning the current item from the list to a loop variable. We used semicolons to separate the for loop itself, the do command that marks the interior of the loop, and the done , which lets us know the loop is over. Alternatively, we could also enter them as technically separate lines.
In this example, the leading angle brackets aren't something you type—they're a prompt supplied by Bash itself. This lets you know you're still inside a loop despite having hit Enter . And that's important, because you can have as many commands as you'd like inside the loop itself!
As you can see, we can string as many commands as we'd like together inside our for loop. Within the loop, each command may refer to the loop variable if it likes—or it can ignore the loop variable entirely.
So far, we've only looked at numbers in sequences, but the for loop doesn't care about that a bit. Any sequence of items, in any order, will work:
Finally, you don't have to supply the list with a variable; you can also embed it directly in the loop... but be careful about how you use quotes, because bash considers a quoted "list" to be a single item:
reader comments
Channel ars technica.
- Articles Automation Career Cloud Containers Kubernetes Linux Programming Security
Introduction to Linux Bash programming: 5 `for` loop tips
%t min read | by Nathan Lager (Sudoer, Red Hat) , Ricardo Gerardi (Editorial Team, Sudoer alumni, Red Hat)

Every sysadmin probably has some skill they've learned over the years that they can point at and say, "That changed my world." That skill, or that bit of information, or that technique just changed how I do things. For many of us, that thing is looping in Bash. There are other approaches to automation that are certainly more robust or scalable. Most of them do not compare to the simplicity and ready usability of the for loop, though.
If you want to automate the configuration of thousands of systems, you should probably use Ansible. However, if you're trying to rename a thousand files, or execute the same command several times, then the for loop is definitely the right tool for the job.
[ You might also like: Mastering loops with Jinja templates in Ansible ]
If you already have a programming or scripting background, you're probably familiar with what for loops do. If you're not, I'll try to break it down in plain English for you.
The basic concept is: FOR a given set of items, DO a thing.
The given set of items can be a literal set of objects or anything that Bash can extrapolate to a list. For example, text pulled from a file, the output of another Bash command, or parameters passed via the command line. Converting this loop structure into a Bash script is also trivial. In this article, we show you some examples of how a for loop can make you look like a command line hero, and then we take some of those examples and put them inside a more structured Bash script.
Basic structure of the for loop
First, let's talk about the basic structure of a for loop, and then we'll get into some examples.
The basic syntax of a for loop is:
The variable name will be the variable you specify in the do section and will contain the item in the loop that you're on.
The list of items can be anything that returns a space or newline-separated list.
Here's an example:
That's about as simple as it gets and there isn't a whole lot going on there, but it gets you started. The variable $name will contain the item in the list that the loop is currently operating on, and once the command (or commands) in the do section are carried out, the loop will move to the next item. You can also perform more than one action per loop. Anything between do and done will be executed. New commands just need a ; delimiting them.
Now for some real examples.
Renaming files
This loop takes the output of the Bash command ls *.pdf and performs an action on each returned file name. In this case, we're adding today's date to the end of the file name (but before the file extension).
To illustrate, run this loop in a directory containing these files:
The files will be renamed like this:
In a directory with hundreds of files, this loop saves you a considerable amount of time in renaming all of them.
Extrapolating lists of items
Imagine that you have a file that you want to scp to several servers. Remember that you can combine the for loop with other Bash features, such as shell expansion, which allows Bash to expand a list of items that are in a series. This can work for letters and numbers. For example:
Assuming your servers are named in some sort of pattern like, web0 , web1 , web2 , web3 , you can have Bash iterate the series of numbers like this:
This will iterate through web0 , web1 , web2 , web3 , and so forth, executing your command on each item.
You can also define a few iterations. For example:
You can also combine iterations. Imagine that you have two data centers, one in the United States, another in Canada, and the server's naming convention identifies which data center a server HA pair lived in. For example, web-us-0 would be the first web server in the US data center, while web-ca-0 would be web 0's counterpart in the CA data center. To execute something on both systems, you can use a sequence like this:
In case your server names are not easy to iterate through, you can provide a list of names to the for loop:
You can also combine some of these ideas for more complex use cases. For example, imagine that you want to copy a list of files to your web servers that follow the numbered naming convention you used in the previous example.
You can accomplish that by iterating a second list based on your first list through nested loops. This gets a little hard to follow when you're doing it as a one-liner, but it can definitely be done. Your nested for loop gets executed on every iteration of the parent for loop. Be sure to specify different variable names for each loop.
To copy the list of files file1.txt, file2.txt, and file3.txt to the web servers, use this nested loop:
More creative renaming
There might be other ways to get this done, but remember, this is just an example of things you can do with a for loop. What if you have a mountain of files named something like FILE002.txt , and you want to replace FILE with something like TEXT . Remember that in addition to Bash itself, you also have other open source tools at your disposal, like sed , grep , and more. You can combine those tools with the for loop, like this:
Adding a for loop to a Bash script
Running for loops directly on the command line is great and saves you a considerable amount of time for some tasks. In addition, you can include for loops as part of your Bash scripts for increased power, readability, and flexibility.
For example, you can add the nested loop example to a Bash script to improve its readability, like this:
When you save and execute this script, the result is the same as running the nested loop example above, but it's more readable, plus it's easier to change and maintain.
You can also increase the flexibility and reusability of your for loops by including them in Bash scripts that allow parameter input. For example, to rename files like the example More creative renaming above allowing the user to specify the name suffix, use this script:
In this script, the user provides the source file's prefix as the first parameter, the file suffix as the second, and the new prefix as the third parameter. For example, to rename all files starting with FILE , of type .txt to TEXT , execute the script like this :
This is similar to the original example, but now your users can specify other parameters to change the script behavior. For example, to rename all files now starting with TEXT to NEW , use the following:
[ A free course for you: Virtualization and Infrastructure Migration Technical Overview . ]
Hopefully, these examples have demonstrated the power of a for loop at the Bash command line. You really can save a lot of time and perform tasks in a less error-prone way with loops. Just be careful. Your loops will do what you ask them to, even if you ask them to do something destructive by accident, like creating (or deleting) logical volumes or virtual disks.
We hope that Bash for loops change your world the same way they changed ours.
Check out these related articles on Enable Sysadmin

Nathan Lager
Nate is a Technical Account Manager with Red Hat and an experienced sysadmin with 20 years in the industry. He first encountered Linux (Red Hat 5.0) as a teenager, after deciding that software licensing was too expensive for a kid with no income, in the late 90’s. Since then he’s run More about me

Ricardo Gerardi
Ricardo Gerardi is Technical Community Advocate for Enable Sysadmin and Enable Architect. He was previously a senior consultant at Red Hat Canada, where he specialized in IT automation with Ansible and OpenShift. More about me
Try Red Hat Enterprise Linux
Download it at no charge from the red hat developer program., related content.


IMAGES
VIDEO
COMMENTS
bash shell script two variables in for loop Ask Question Asked 11 years, 2 months ago Modified 3 months ago Viewed 109k times 11 I am new to shell scripting. so kindly bear with me if my doubt is too silly.
bash allows for loops with more than one variable, but only in C like syntax: for ( (i=0,j=10;i<=j;i++,j--)) do echo "i=$i" echo "j=$j" done Share Improve this answer Follow answered Apr 28, 2016 at 15:16 Dani_l 4,720 1 18 34 2 how are i and j read from $INPUTFILE ? - Archemar Apr 28, 2016 at 15:43 1
One way to do it: #! /bin/sh VAR1="abc def ghi" VAR2="1 2 3" fun () { set $VAR2 for i in $VAR1; do echo command "$i" "$1" shift done } fun Output: command abc 1 command def 2 command ghi 3 Share Improve this answer Follow
How to iterate a command with two different variables? Ask Question Asked 6 years ago Modified 1 year, 11 months ago Viewed 7k times 2 Using kernel 2.6.x GNU bash, version 4.3.42 (1)-release (arm-openwrt-linux-gnu) In a bash script, how would you write a for loop that produces the following result with the variables below ?
Two variables in for loop in bash Ask Question Asked 2 years, 11 months ago Modified 2 years, 11 months ago Viewed 409 times 0 for file in /root/* do if [ "$file" != "/root/00" ] && [ "$file" != "/root/01" ] then echo $file fi done in this code, as you see i try to get the files in the /root folder the output should be like this:
Advantages: Keeps it clear which server name corresponds with which fqdn. Its pure bash & uses no external anything. . If the server name is always going to be the first part of the fqdn, the array can be even simpler and you can have bash figure the server name out from the fqdn.
Jan 12, 2020 at 1:06 Add a comment 1 Answer Sorted by: 7 You need to iterate over the array index number and not the value. This is done using the $ {!folder [@]} syntax. In that way you can access both arrays via the same index inside the loop e.g. (putting echo in front for this example)
Using multiple variables in a for loop Ask Question Asked 6 years, 9 months ago Modified 6 years, 1 month ago Viewed 15k times 4 I want to use two variables in a for loop like this (This is for example, I'm not going to execute seq like operations) for i j `seq 1 2` 'www.google.com www.yahoo.com';do echo $i $j;done Expected output is
1 Answer Sorted by: 5 Your problem isn't with the loop, it's with the assignment. The variable name needs to be literal in an assignment, i.e. you can write title=some_value but not $arg=some_value. A portable way to assign to a variably-named variable is to use eval.
@SummerEla: You could $(seq 0 9); in Bash, you could write for i in {0..9} too. There are lots of ways that could be used. The advantage of not using seq is that it saves on processes (11 of them). You're unlikely to find that's a problem, but it is a nominal reason for using one of the alternative notations (the one in the answer, or the brace expansion).
12 Is there a way to specify multiple variables (not just integers) in for loops in bash? I may have 2 files containing arbitrary text that i would need to work with. What i functionally need is something like this: for i in $ (cat file1) and j in $ (cat file2); do command $i $j; done Any ideas? bash command-substitution for Share
How to pass multiple variable to a for loop as a argument in a bash script? [closed] Asked 3 years, 3 months ago Modified 3 years, 3 months ago Viewed 8k times 1 Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
@Paillou : So that "other" command also establishes the association, which bam file "belongs to" which xml file? Usually, such associations are represented in bash by an associative array, but you did not show how the other command generates the two file lists. Maybe a good solution would involve change that command.
How do I write a 'for' loop in Bash? Ask Question Asked 14 years, 11 months ago Modified 2 years ago Viewed 147k times 136 I'm looking for the basic loop like: for (int i = 0; i < MAX; i++) { doSomething (i); } but for Bash. bash for-loop iterator Share Improve this question Follow edited Aug 28, 2021 at 19:37 Peter Mortensen 30.7k 21 105 131
BTW, an alternative approach would be to loop over a single array of pairs (using a suitable separator), i.e. (164,16 164,19....).Of course you have to tear apart the pair into its consituents when using it in the invocation of your shell script, but it has the advantage that we can easily see, which value is paired up with which other value, so in the end, it would perhaps be more maintainable.
The versatile Bash for loop does much more than loop around a set number of times. We describe its many variants so you can use them successfully in your own Linux scripts. The for Loop All scripting and programming languages have some way of handling loops. A loop is a section of code that you want to have executed repeatedly.
Another option, in bash v4+ is to read the two files into 2 arrays, then echo the array elements side-by-side: ... How do I write a for loop with 2 variables in bash? 0. bash - for loop different iterations. 0. Bash combining two for loops. 0. for loop at the bash commandline. 5.
Bash handles variables a bit oddly compared to any of the "real" languages I've used. In particular, you must refer to the same variable with different syntax when setting its value versus...
The basic syntax of a for loop is: for <variable name> in <a list of items>;do <some command> $<variable name>;done; The variable name will be the variable you specify in the do section and will contain the item in the loop that you're on. The list of items can be anything that returns a space or newline-separated list.
So the line: Code: if [ $# -eq 1 ] && [ -f $1 ]; then. Means: if [ the number of parameters passed to the script is exactly 1 ] AND [ the parameter is a path to a valid file ] ; then we attempt to read lines from the file and extract data until we reach the end of the file.
Basically, a for loop performs a task for a specific set of items while a given condition is true.. Let's see the basic syntax of a for loop in Bash:. for <name_of_variable> in <list_of_items>; do <perform_some_task> done; A simple use case involving for loops is number iteration. Using the above syntax, we can iterate over a range of numbers, say 1 to 10: