Faster Learning Tutorials

Linux bash for loop with examples

for allows you to create a program loop within a script (for is technically a bash keyword, not a command).
  1. Purpose - Learn what a for loop is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with a for loop.
  4. Script - Add a for loop to our script and run it.
  5. A tip - Finish off with one more insight.
Paul Alan Davis, CFA, November 22, 2016
Updated: July 25, 2018
In this tutorial, 98 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Next

~/ home  / tech  / linux essentials  / for command

The Linux for command (keyword)


Learn how to build in program logic at the Linux command line.

Video Tutorial

Linux bash for loop with examples (4:00)

Videos can also be accessed from the YouTube Playlist.

Video Script

The Command and Why You Need It

Our ninety-eighth word, or command (keyword) to memorize is for from our category Workflow.

for allows you to create a program loop (like in a shell script).

Recall from video (tutorial) #96, we saw our first looping example with while, and now we'll see a second, for, which is helpful to count iterations, and it is often used in Finance.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The for command (keyword) has no traditional options and no arguments as it, like others in the Workflow category, is technically a keyword and comes with other keywords, in this case do and done.

Unlike most commands, help is not available with double-dash --help, as for is a shell built-in covered in the bash manual page.

So why is for an important command (keyword)? Well, another looping tool is required for (counting) sequences. And now you know how to do that.


Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Let's review those 4 words in this group.

$ type for; type in; type do; type done for is a shell keyword in is a shell keyword do is a shell keyword done is a shell keyword

See they're all keywords.

Next, let's try a few one-liners at the command line. It goes like this for variable, commonly we use the letter i as the variable, in put in a range or pattern, then you do commands, and then you're done.

$ for i in a b c d; do echo $i; done a b c d

Also patterns can be letters like this or they can also be integers. And interestingly, you'd assume they'd go in order.

$ for i in 1 2 3 4; do echo $i; done 1 2 3 4

They don't have to be in the correct order.

$ for i in 2 4 8 9; do echo $i; done 2 4 8 9 $ clear

Then, let's use a second method here for coding loops. There are two. This one is more common in math, and it goes like this: for ((i=1; i<9; i=i+1));.

$ for ((i=1; i<9; i=i+1)); do echo $i; done 1 2 3 4 5 6 7 8

So that whole pattern there in double parentheses, semi-colon do, then echo $i each time it goes through that loop, the $i then the done. And you can see that. That mirrors what we've seen before (tutorial #96 on while).

And then last, remember we'll return to funscript in the last video (tutorial) of the Playlist, sticking with FactorPad's specialty: numbers.

A Final Tip

Okay now you know how to use for. And you know the syntax for commands, options and arguments.

One last tip about the for command (keyword). So if you noticed similarities between the second form of for and the while loop, good catch.

Okay, thanks for visiting today. I hope this was a helpful introduction to the for command (keyword).

Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:

What's Next?

Keep advancing your knowledge and check out what's new at our YouTube Channel. Subscribe here.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the case command, hit Back.
  • To learn one more command until the grand finale, click Next.

Outline Back Next

~/ home  / technology  / linux essentials  / for command

Linux for
shell for
bash for loop
shell script for
linux shell for
shell script loop
bash counter
bash programming
bash for in
for in do done
incremental counter
bash for i in
loop bash
for keyword