Bash printf: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * bash * printf")
 
No edit summary
 
Line 1: Line 1:
=External=
* http://wiki.bash-hackers.org/commands/builtin/printf
=Internal=
=Internal=


* [[bash#Built-In_Commands|bash]]
* [[bash#Built-In_Commands|bash]]
* [[printf#Subjects|printf]]
* [[printf#Subjects|printf]]
=Overview=
Prints the arguments according to the specified format:
<pre>
    printf "something is string: %s, something else is decimal %d and something is a float: %f \n"  "${var1}" "${var2}" "${var3}"
</pre>
=Float Format=
Specify how many digits to display for the '''entire''' number and how many digits to use for the portion after the decimal point. If less than the specified number of digits are available, the result will be padded with spaces. If more than the specified number of digits are provided, the final result will expand:
<pre>
      %10.2f
</pre>
Left-bound string (default is right bound)
<pre>
      %-10.2f
</pre>
=String Format=
Specify total length to print on (by default it justifies to the right):
<pre>
    printf "%50s\n" "This field is 50 characters wide and right-justified..."
</pre>
Left-bound printing:
<pre>
    printf "%-50s\n" "This field is 50 characters wide and left-justified ..."
</pre>

Latest revision as of 19:42, 23 April 2016

External

Internal

Overview

Prints the arguments according to the specified format:


    printf "something is string: %s, something else is decimal %d and something is a float: %f \n"  "${var1}" "${var2}" "${var3}"

Float Format

Specify how many digits to display for the entire number and how many digits to use for the portion after the decimal point. If less than the specified number of digits are available, the result will be padded with spaces. If more than the specified number of digits are provided, the final result will expand:

       %10.2f

Left-bound string (default is right bound)

       %-10.2f

String Format

Specify total length to print on (by default it justifies to the right):

     printf "%50s\n" "This field is 50 characters wide and right-justified..."

Left-bound printing:

     printf "%-50s\n" "This field is 50 characters wide and left-justified ..."