Wednesday, July 11, 2007

Lame But Useful Bash Tricks (Part 1)

Incrementing Letters and Numbers for Variable Lists

[Note: Applicable for Bash version 3.00]

Using curly brackets "{" and "}", you can set a range with a variable list. Inside the curly brackets, the range just needs to be separated by two periods, "..".

Ex.
$ SERVERLIST="`echo server-{1..5}`"
$ echo $SERVERLIST
server-1 server-2 server-3 server-4 server-5

$ SERVERLIST="`echo server-{a..d}`"
$ echo $SERVERLIST
server-a server-b server-c server-d

This could also be useful for quick "for" loops with a list that has incremental numbers off the same root name.

Ex.
for i in server-{1..5}
do
echo $i
done

server-1
server-2
server-3
server-4
server-5

No comments: