Bash printf

From NovaOrdis Knowledge Base
Jump to: navigation, search

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 ..."