Home > bash > Printing variables side by side – Part 2

Printing variables side by side – Part 2

Problem:

You have a for loop and you want to print the results side by side. Echo will not work in this situation.

Solution:

printf

Here is the example with echo.

shell>echo -e "trains\nplanes\nautomobiles" | \
while read tran_mode;
do
echo $tran_mode;
mysql -uUser -pPassword database -N \
 -e"select count(*) from table where value=$tran_mode";
done

trains
0
planes
0
automobiles
0

Here it is with printf

shell>echo -e "trains\nplanes\nautomobiles" | \
while read tran_mode;
do
printf "%s, %s" $tran_mode;
mysql -uUser -pPassword database -N \
 -e"select count(*) from table where value=$tran_mode";
done

trains, 0
planes, 0
automobiles, 0
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

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

%d bloggers like this: