Using Perl range operators within Shell scripts

While this might be totally redundant, I couldn’t for the love of God remember how range operators were used within the various shells (and specifically the Korn shell). Tried various tricks but I guess I must’ve gotten “perl-ized” somewhere along the way. So here’s my workaround for quick shell scripts (usually fired off from the command line on the fly) that require range operators (say you want to iterate through the numbers 1 – 100 and do some activity with each value.

for i in `perl -e ‘foreach $i (1..100) { print “$i \n”; }’`
echo “I is $i”;

Here we’ll invoke a perl one-liner to print out the values of $i in the range 1 to 100. We read that and do something with it within a “ksh for loop”. It’s easier than having to type in:

for i in 1 2 3 4 5 …. 100
echo “I is $i”

Sidenote: Hopefully I’ll get to update this blog more often that I usually do — if there’s no one watching, at least it’s something for me to refer to (as a cheatsheet on the fly).

1 thought on “Using Perl range operators within Shell scripts”

Leave a Reply to Anonymous Cancel Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.