Format and print data.
Write the formatted arguments to the standard output under the control of the format.

      printf [-v var] format [argument]...

      printf --help

      printf --version


   -v   Cause the output to be assigned to the variable var rather than being printed to the standard output. 

   The format characters and their meanings are:

       \"     double quote

       \0NNN  character with octal value NNN (0 to 3 digits)

       \\     backslash

       \a     alert (BEL)

       \b     backspace

       \c     produce no further output

       \f     form feed

       \n     new line

       \r     carriage return

       \t     horizontal tab

       \v     vertical tab

       \xNNN  byte with hexadecimal value NNN (1 to 3 digits)

       \uNNNN character with hexadecimal value NNNN (4 digits)

              character with hexadecimal value NNNNNNNN (8 digits)

       %%     a single %

       %b     ARGUMENT as a string with '\' escapes interpreted

       %Wd    Integer 'W' digits wide xxxx

       %W.De  Scientific notation x.xxxx e nnn. float, double

       %W.Df  Fixed format xx.xxxx. float, double

       %W.Dg  Variable 'W' digits wide,'D' decimals  xxxx.xx

       %q     Output the corresponding argument in a format that can be
              reused as shell input

              Output the date-time string resulting from using datefmt as a format string for strftime(3).
              The corresponding argument is an integer representing the number of seconds since the epoch.
              Two special argument values may be used: -1 represents the current time, and -2 represents
              the time the shell was invoked.
              If no argument is specified, conversion behaves as if -1 had been given.
              This is an exception to the usual printf behavior. 

       %s     Character string char

       and  all C format specifications ending with one of diouxXfeEgGcs, with
       ARGUMENTs converted to proper type first.  Variable widths are handled.
       e.g.  '\0ooo' = an octal number, '\xhhh' = a hex number

The format is a character string which contains three types of objects:

The format is reused as necessary to consume all of the arguments. If the format requires more arguments than are supplied, the extra format specifications behave as if a zero value or null string, as appropriate, had been supplied.

The return value is zero on success, non-zero on failure.


Print the decimal number 5 followed by a newline (\n):
$ printf "%d\n" 5

Print as float (default 6 decimal places):
$ printf "%f\n" 5

Print text followed by variable $USER
$ printf "Hello, $USER.\n\n"

Loop through a text file 'demo.txt' and print each line:

while IFS= read -r line; do
printf '%s\n' "$line"
done <demo.txt

Print multiple lines:
$ printf %s "\
with quotes we can echo
several lines at a time

Display variables
$ distance=15
$ printf "Distance is %5d Miles\n" $distance
Distance is 15 Miles

Echo a list of numbers from 1 to 100, adding 3 digits of Zero padding so they appear as 001, 002, 003 etc:
for ((num=1;num<=100;num+=1)); do echo 'printf "%03d" $num'; done

Use \n anywhere to start a new line:

$ printf "Two separate\nlines\n"
Two separate

Print Unix Epoch time in seconds and save in the variable epochtime:
$ printf -v epochtime "%(%s)T"

In bash version 5, you can use $EPOCHREALTIME with microsecond accuracy.

Print decimal numbers interspersed with text
$ printf "There are %d orders valued at over %d euros.\n" 64 1500
There are 64 orders valued at over 1500 euros.

Print text interspersed with command results:
$ printf "This is 'uname -s' running on a 'uname -m' processor.\n\n"

Convert a hex number to decimal:
$ printf "%d\n " 0xF

Convert a decimal number to Hex:
$ printf "0x%X\n " 15

Convert a decimal number to Octal:
$ printf "0%o\n " 8

Convert an Octal number to decimal:
$ printf "%d\n " 010

“Fortune favours the bold, Fortune favours the brave” ~ Latin proverb

Related linux commands

cat - Display the contents of a file.
less - Display output one screen at a time.
more - Display output one screen at a time.
Equivalent Windows commands: ECHO - Display message on screen.

Copyright © 1999-2024
Some rights reserved