Linux / Unix: Bash Shell Assign Printf Result To Variable

by on August 29, 2011 · 0 comments· last updated at August 29, 2012

How do I assign printf command result to a shell variable under Unix like operating systems?

The printf command syntax is as follows:

 
printf "FORMAT" var
var=$(printf "FORMAT" var1)
var="$(printf "FORMAT" var1)"
 

Examples

Type the following command:

 
dir="/home/httpd"
printf "%s\n" $dir
 

To store result to a variable called j, enter:

 
dir="/home/httpd"
j=$(printf "%s" $dir)
echo "$j"
printf "%s\n" $j
 


You should follow me on twitter here or grab rss feed to keep track of new changes.

Featured Articles:

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Tagged as: , , , ,

Previous Faq:

Next Faq: