Home > bash > bash loop without for

bash loop without for

The following examples require bash >= 3.0.

Example 1

Print numbers from 1 to 10 without a for loop:

echo {1..10}

Output:

1 2 3 4 5 6 7 8 9 10

What happens is bash expands the range this way:

echo 1 2 3 4 5 6 7 8 9 10

Example 2

Produce file names from 01.pdf to 10.pdf:

echo {01..10}.pdf

Output:

01.pdf 02.pdf 03.pdf 04.pdf 05.pdf 06.pdf 07.pdf 08.pdf 09.pdf 10.pdf

Example 3

Download a gallery of images with wget:

wget http://example.com/gallery/image{01..10}.jpg

bash will expand it like this:

wget http://example.com/gallery/image01.jpg http://example.com/gallery/image02.jpg http://example.com/gallery/image03.jpg http://example.com/gallery/image04.jpg http://example.com/gallery/image05.jpg http://example.com/gallery/image06.jpg http://example.com/gallery/image07.jpg http://example.com/gallery/image08.jpg http://example.com/gallery/image09.jpg http://example.com/gallery/image10.jpg

Related posts

About these ads
Categories: bash Tags: , ,
  1. February 4, 2011 at 10:43

    Woah, this is nice !

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 72 other followers

%d bloggers like this: