Shell printf formatted output statement

Source: Internet
Author: User
Tags echo command posix

The printf command is used to format the output and is an enhanced version of the echo command. It is a finite variant of the C-language printf () library function, and is somewhat different in syntax.

Note: printf is defined by the POSIX standard, and portability is better than echo.

As with the echo command, the printf command can also output a simple string:

    1. $printf "Hello, Shell\ n"
    2. Hello, Shell
    3. $

printf does not wrap like echo, and you must explicitly add a line break (\ n).

The syntax of the printf command:

printf  format-string  [arguments ...]

Format-string is the format control string and arguments is the parameter list.

printf () has been described in the C-language introductory tutorial, features and usage similar to the printf command, see: C-Format output function printf ()

This is only a description of the difference from the C-language printf () function:

    • printf command without parentheses
    • Format-string can be without quotation marks, but it is best to add single quotes and double quotes.
    • When the parameter is more than the format control (%), format-string can be reused and all parameters can be converted.
    • Arguments use spaces separated by commas.


Take a look at the following example:

  1. # format-string is double quotes
  2. $ printf "%d%s\ n" 1 "abc"
  3. 1 ABC
  4. # single quotes are the same as double quotes
  5. $ printf '%d%s\ n' 1 "abc"
  6. 1 ABC
  7. # No quotation marks can also be output
  8. $ printf %s abcdef
  9. ABCdef
  10. # The format specifies only one parameter, but the extra parameter is still output in that format, format-string is reused
  11. $ printf %s ABC def
  12. ABCdef
  13. $ printf "%s\ n" abc def
  14. Abc
  15. Def
  16. $ printf "%s%s%s\ n" A b c D e F g h i j
  17. A b C
  18. D E F
  19. G h I
  20. J
  21. # If there is no arguments, then%s is replaced with NULL,%d replaces with 0
  22. $ printf "%s and%d \ n"
  23. and 0
  24. # If the string is displayed in%d format, then there will be a warning indicating an invalid number, at which point the default is 0
  25. $ printf "The first program always prints '%s,%d\ n'" Hello Shell
  26. -bash: printf: Shell: Invalid number
  27. The first program is always prints ' hello,0 '
  28. $


Note that, according to the POSIX standard, floating-point formats%E,%E,%f,%g, and%g are "No need to be supported." This is because awk supports floating-point budgeting and has its own printf statement. In this way, a small awk program can be used to print a floating-point value in a shell program. However, the printf commands built into bash, ksh93, and zsh support floating-point formatting.

Shell printf formatted output statement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.