Home > bash > Printing variables side by side – part 1

Printing variables side by side – part 1

Problem:
Print variables side by side.

Solution:
printf command For the example below echo would be best. But for a better example of when you might want to use printf read this.

shell>val1="cat"
shell>val2="dog"
shell>echo "$val1,$val2"
cat, dog
shell>printf "%s, %s" $val1 $val2
cat, dog
shell>printf "The values are %s and %s." $val1 $val2
The values are cat and dog.

Cases where you would want to use printf instead of echo are explained here
http://stackoverflow.com/questions/3652524/bash-echo-string-that-starts-with

Update. (2012-04-20)

A better example of using printf

Advertisements
Categories: bash Tags:
  1. Hai
    July 17, 2011 at 7:11 am

    what is wrong with the echo command?
    echo “$var1, $var2” will accomplish the same without the use of external command printf.

    • July 18, 2011 at 5:38 am

      True. echo works just as well. Thanks.

  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: