Bash One Liner that uses multiple variables

I’m not sure if this particular post will come in handy to a lot of people, but my bash scripting is still pretty weak and I think the best way to commit this to memory will be to write a short post on it.

I find myself doing one line “for” statements from the command line all the time to make quick loops. Maybe I need to loop through a list of servers and delete them all, maybe I need to ping a group of servers and see if they all reply, maybe I need to build a lot of servers of different flavors at once, etc.

Whatever the reason, I have occasionally come across the need for multiple variables in my loop. For example, let’s say that I have a list of data like this:

That format may look familiar if you use the Python Command Line tool for Rackspace Cloud Servers (

Now let’s say that I want to create an image of the 8 servers listed above. I would usually put the above table in a tmp file, and just call out the Server ID. Something like:

This gets the job done, but it is pretty ugly. I end up with image names like “Image-of-11114”, which can be more difficult to read than “Image-of-Cherry”

Using something like the following, I can allow multiple variables in my for loop:

The whole idea is to read the full line into a variable (x) and then loops through the line assigning a specific variable to whichever fields I need.

  1. while loops have built-in support for assigning multiple, whitespace separated variables, try something like this:

    while read x id x name x; do echo "myservers image-create $id Image-of-$name"; done < temp

    I am using x as a placeholder for the | characters in the table, and the last x nabs up the rest of the line after the name, end result is that you get the variables $ip and $name, and it looks cleaner to boot!

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">