Terms of Service Privacy Policy Contact Us



    printf is common command in programming languages and shell scripting. It's origin is C but it has branched out to many others.

    In shell scripting printf is the Cadillac version of echo. For sending plain text to stdout, echo is a good choice however for any form of formatting printf is the way to go.

    CREATED 2017-08-29 07:16:00.0


    UPDATED 2017-08-29 07:16:00.0


    $ printf "Hi my name is: %s " "Kenny" Hi my name is Kenny

    $ printf "The current temprature is: %.1f " "86.4"
    The current temprature is: 86.4

    printf <format> <arguments>

    CREATED 2017-08-29 09:58:01.0


    UPDATED 2017-08-29 09:58:06.0


    $ printf "This is test %d " $testNo This is test 25
    $ testNo=25
    $ printf "%s " "This is test $testNo "

    This is test 25
    $ printf "%s " "This is test 25 "

    This is test 25

    The format is how and what to print. It can be a mixture of static text and formats or it can be just formats OR just static text.

    Static text mixed with formats...

    ...or just formats...

    ...OR just static text...

    CREATED 2017-08-29 10:42:23.0


    UPDATED 2017-08-29 16:44:24.0


    %% Print a percent sign.
    %a %A Prints a double in Hexadeciaml Floating point Literal. The capitol version prints capatol letters.
    %b Allows the interpretation of backslashes
    %c Character. It only prints one. e.g. printf "%c " "Test" will print T.
    %d Decimal (unsigned)
    %e %E Double printed in scientific notation e.g. 1.254343e+04. The capitol E prints letters in uppercase
    %f Floating point number
    %g %G Does different things depending on the size and the number of decimals. e.g. 123456789.0 is printed as 1.23457e+08 while 123456.7890 is printed as 123457. The capatolized version of g prints letters in uppercase.
    %i Prints an integer (same as %d)
    %n Prints the number of characters printed at the that point and stores it in a variable. e.g. printf "This is a %ntest " x will store the value 10 in x.
    %o Unsined Octal
    %q Quotes the arg
    %s Prints a string.
    %(FORMAT)T Prints a time in FORMAT format. See strftime Formats below for details.
    %u Unsigned Decimal
    %x Unsined Hexadecimal
    %X Unsined Hexadecimal with uppercase digits

    CREATED 2017-08-30 13:00:29.0


    UPDATED 2017-08-30 13:00:29.0

    strfTime Formats

    • %a - Abbreviated weekday name (Mon, Tue, etc)
    • %A - Full weekday name (Monday, Tuesday, etc)
    • %b - Abbreviated name of the month (Jan, Feb, etc)
    • %B - Month name (January, February, etc)
    • %c - Preferred date/time representation
    • %C - Century as a 2-digit integer
    • %d - The day of the month.
    • %D - Date in m/d/y
    • %e - Date in m/d/y except leading 0's are spaces
    • %F - Date in yyyy/mm/dd
    • %G - Week year
    • %g - 2-digit week year
    • %h - Abbreviated name of the month
    • %H - Hour in 24 hour format
    • %I - Hour in 12 hour format
    • %j - Julian date (day of the year)
    • %k - Hour in 24 hour format with no leading zero
    • %l - Hour in 12 hour format with no leading zero
    • %m - Number of the month
    • %M - Minute of the hour
    • %p - AM/PM
    • %P - am/pm
    • %r - Time with am/pm added
    • %R - 24-hour time no seconds
    • %s - Seconds since the Epoch
    • %S - Seconds of the minute
    • %T - 24-hour time H:M:S
    • %u - Number of the day of the week
    • %U - Number of the week (0..52)
    • %V - Week number of the year
    • %w - Number of the day of the week (0-6)
    • %W - Week number of the year
    • %x - Preferred date format
    • %X - Preferred time format
    • %y - 2-digit year
    • %Y - 4-digit year
    • %z - Timezone offset
    • %Z - Timezone Name

    CREATED 2017-08-31 14:32:27.0


    UPDATED 2017-08-31 14:55:32.0


    $ printf "This is a (%*s) " 20 "Test" This is a (               Test) printf "(%-20s) " "Hello" (Hello               ) printf "(%+20s) " "Hello" (               Hello)

    Modifiers are symbols used with formats to make life easier.

    * Takes the width as the first argument
    $ printf "(%*s) " "10" "Hello"
    (Hello     )
    . forces the field to a certain width. The field will not be expanded if it is longer than the width. e.g.
    $ printf "%.9s" "fieldToBePrinted "
    # For different number base output...
    • %#0 - prints octal numbers with a leading zero > 0
    • %#x or%#X - prints hex numbers with a preceeding ox
    • %#g or %#G - prints float with trailing zeros up to the percisin
    - flushes text to the left, default is right
    0 pads numbers with zeros instead of spaces
    space prints sign (-) for neg, space for pos.
    + prints sign of numbers pads left for characters

    Variable width - the astrisk (*). The astirsk allows the field width to be passed as an argument.

    Pads the left side of the field to make the width 20 characters.

    The plus (+) and minus (-) are used to indicate which side to justify the string. Minus pads the right side of the string i.e. placing the text on the left while plus pads the left side of the string putting the text on the right.



    The default is plus so %20s and %+20s are the same thing.

    CREATED 2017-08-29 07:16:03.0


    UPDATED 2017-08-31 14:56:46.0


    $ printf "First Arg (%s), Second Arg (%s) " "First"
    First Arg (First), Second Arg ()

    $ printf "First Arg (%d), Second Arg (%d) " 1
    First Arg (1), Second Arg (0)
    $ printf "First Arg(%s), Second Arg(%s) " "First" "Second" "Third"
    First Arg(First), Second Arg(Second)
    First Arg(Third), Second Arg()

    The arguments follow the format string. There should be an argument for every format used. If not, printf uses an empty string or a 0.

    If there are too many arguments and not enough formats, printf produced another line to use all the arguments.

    CREATED 2017-08-29 10:05:10.0


    UPDATED 2017-08-31 14:56:43.0


    Page Server: Ithica

    DBID: db.wam

    ©2012 Leistware Data Systems

        Hello anonymous