An ad-free and cookie-free website.
Learn how to build in program logic at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux bash for loop with Examples (4:00)
Our ninety-eighth word, or command (keyword) to memorize is
for from our category
for allows you to create a program
loop (like in a shell script).
|None for Linux for|
Recall from video (tutorial) #96, we saw our first looping example
while, and now we'll see a
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).
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
Unlike most commands, help is not available with double-dash
for is a shell built-in
covered in the
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.
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
Also patterns can be letters like this or they can also be integers. And interestingly, you'd assume they'd go in order.
They don't have to be in the correct order.
Then, let's use a second method here for coding loops. There are
two. This one is more common in math, and it goes
for ((i=1; i<9; i=i+1));.
So that whole pattern there in double parentheses, semi-colon
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
And then last, remember we'll return to
funscript in the last video
(tutorial) of the Playlist, sticking with FactorPad's specialty:
Okay now you know how to use
And you know the syntax for commands, options and arguments.
One last tip about the
(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).
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
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.
casecommand, hit Back.
untilthe grand finale, click Next.
A newly-updated free resource. Connect and refer a friend today.