/ factorpad.com / tech / linux-essentials / for-command.html
An ad-free and cookie-free website.
Intermediate
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
Workflow.
for
allows you to create a program
loop (like in a shell script).
-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.
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
done
.
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
like this: for ((i=1; i<9; i=i+1));
.
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.
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).
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.
case
command,
hit Back.until
the
grand finale, click Next./ factorpad.com / tech / linux-essentials / for-command.html
A newly-updated free resource. Connect and refer a friend today.