Getting Loopy with Bash: using for loops

FAIL (the browser should render some flash content, not this).

Download in .ogv format


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.


Guy Stalnaker's picture

And no sooner to I post here than I find the answer on another site, ShellGeek (imagine that). So, thanks anyway.


Using for at the prompt

Guy Stalnaker's picture

I was trying to come up with a way, using for, to run an executable on the output of the ls command in a directory. That is, to have the output of ls be the input to a command:

#> for m in `ls`; do echo $m; done

This works ONLY if the file names in the directory have no spaces, e.g.,

autosave Documents Music
bin emerald-themes
doc man

But if the filename is a multi-word name with spaces, the for construct assigns to m the individual words and the echo output breaks the filename across separate lines. I suppose this has more to do with how ls outputs, but I cannot find an ls option (not -Q nor -1 nor -x) that seems to get the for construct to 'think' that the filenames output by ls are legitimate if they have spaces (if that makes any sense).

I have found a way to use find's -exec or -print0 with xargs to do what I wanted to do, but I was curious if any of you gurus at linux journal can come up with a way of using for on the command line:

for x in '{command with output}'; do {command on output} $x; done

for m in `ls`;do command

Anonymous's picture

for m in `ls`;do command "$m";done

putting $m inside " marks causes $m to be a single argument. I do this all the time when I used Cygwin on Windows (at work).


re: it should be noted though

pschmitt52's picture

Isn't this the LINUX Journal after all? :)

it should be noted though

slacker's picture

it should be noted though that calling seq makes it less portable as you wont likely find seq on many flavors of unix... granted you can argue that you wont find bash either but hey, just sayin.

target :)

thogarty's picture

That looks like a Target ball in the intro :)

One of these days you should do some tech tips on how you make these videos (If you have already, please post a link so I can check it out). What hardware and programs you use to record and edit? I would like to make similar instructional videos for some of my personal and work documentation.

Good to see these shell tips. Are there cases when the list would be too long for the shell to handle directly requiring something like xargs? (maximum line length) If so, that may be another good tech tip to post. These tech tips are quickly becoming my favorite part of Linux Journal :). They're concise and informative.

for + seq

pschmitt52's picture

If you want use the for loop as a counting loop
then you could do this:

for x in `seq 1 1000`
    echo "line: $x"

This will run 1000 iterations.

#!/bin/sh # doing it in C

Anonymous's picture


# doing it in C way

for ((I=0;I<=1000;I++))
printf "line: %03d\n" $I

you can also use the

exsnafu's picture

you can also use the following in newer(3+ i think?) versions of bash:

for x in {1..1000}; do
echo "line: $x"

Though Not Quite Interchangeable

Mitch Frazier's picture

Note however that this works:

for i in `seq 1 $j`
    echo $i

Where as this does not:

for i in {1..$j}
    echo $i

To "make" it work do:

for i in `eval echo {1..$j}`
    echo $i

Mitch Frazier is an Associate Editor for Linux Journal.