FactorPad
Build a Better Process

Linux bash for loop with Examples

Linux 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.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux for, 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 Tip Next

/ factorpad.com / tech / linux-essentials / for-command.html


An ad-free and cookie-free website.


Examples of the Linux for Command (Keyword)

Intermediate

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

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux bash for loop with Examples (4:00)

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).

Common Linux for Options
-options description
None for Linux for

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.

Demonstration

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 get reminders of what's new by subscribing at YouTube, following @factorpad on Twitter, and joining our no-spam email list.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / for-command.html


linux for
shell for
bash for loop examples
shell script for
linux shell for
shell script loop
bash counter
bash for in
for shell
linux for loop
bash loop
for in bash
bash for i in
bash incremental counter
loop bash
bash for loop
loop in shell script
for bash

A newly-updated free resource. Connect and refer a friend today.