printf

printf

The printf command in Linux is used to print formatted output to the standard output stream. The printf command is a versatile command that can be used to print a variety of different types of output, including strings, numbers, and dates.

The syntax for the printf command is as follows:

printf format [arguments]

The format argument is a string that specifies the format of the output. The arguments argument is a list of values that are used to fill in the format string.

The printf command supports a variety of different format specifiers. Some of the most common format specifiers include:

  • %s: Prints a string.

  • %d: Prints an integer.

  • %f: Prints a floating-point number.

  • %c: Prints a character.

  • %t: Prints a date or time.

Here is an example of how to use the printf command to print a string:

printf "Hello, world!\n"

This command will print the string "Hello, world!" to the standard output stream.

Here is an example of how to use the printf command to print an integer:

printf "%d\n" 12345

This command will print the integer 12345 to the standard output stream.

Here is an example of how to use the printf command to print a floating-point number:

printf "%f\n" 3.14159

This command will print the floating-point number 3.14159 to the standard output stream.

Here is an example of how to use the printf command to print a character:

printf "%c\n" 'a'

This command will print the character 'a' to the standard output stream.

Here is an example of how to use the printf command to print a date:

printf "%t\n"

This command will print the current date to the standard output stream.

The printf command is a versatile command that can be used to print a variety of different types of output. It is a valuable tool for debugging, logging, and creating formatted output.

help

printf: printf [-v var] format [arguments]
    Formats and prints ARGUMENTS under control of the FORMAT.
    
    Options:
      -v var    assign the output to shell variable VAR rather than
                display it on the standard output
    
    FORMAT is a character string which contains three types of objects: plain
    characters, which are simply copied to standard output; character escape
    sequences, which are converted and copied to the standard output; and
    format specifications, each of which causes printing of the next successive
    argument.
    
    In addition to the standard format specifications described in printf(1),
    printf interprets:
    
      %b        expand backslash escape sequences in the corresponding argument
      %q        quote the argument in a way that can be reused as shell input
      %Q        like %q, but apply any precision to the unquoted argument before
                quoting
      %(fmt)T   output the date-time string resulting from using FMT as a format
                string for strftime(3)
    
    The format is re-used as necessary to consume all of the arguments.  If
    there are fewer arguments than the format requires,  extra format
    specifications behave as if a zero value or null string, as appropriate,
    had been supplied.
    
    Exit Status:
    Returns success unless an invalid option is given or a write or assignment
    error occurs.

Last updated