The printf command of the shell tutorial

Source: Internet
Author: User
Tags echo command

In the previous section we learned about the Shell's echo command, which we'll learn from the Shell's other output command printf.

The printf command mimics the printf () program in the C library.

printf is defined by the POSIX standard, so scripting with printf is better than using echo portability.

printf uses reference text or space-delimited parameters, which can be used outside of the format string in printf, as well as the width of the string, left and right alignment, and so on. Default printf does not automatically add line breaks like echo, we can add \ n manually.

The syntax of the printf command:

printf  format-string  [arguments ...]

Parameter description:

    • format-string: Controlling strings for formatting
    • arguments: as a parameter list

Instance:

printf "%-10s%-8s%-4s\n" name sex weight kg  

Execution Result:

[[email protected] bin]#/test.sh name     sex   weight kg Guo jing     male      66.12 yang over     male      48.65 Guoff     female      47.99

%s%c%d%f are format alternates

%-10s refers to a width of 10 characters (-for left alignment, not to right-aligned), any character will be displayed in 10 character justifies characters, if not enough is automatically filled with spaces, more than will also show the content.

%-4.2F refers to the format of decimals, where. 2 refers to 2 decimal places reserved.

More examples:

Execution Result:

1 ABC1 Abcabcdefabcdefabcdefa b CD e fg H IJ and   0

Escape sequences for printf

sequence Description
\a A warning character, usually an ASCII bel character
\b Back off
\c Suppresses (does not display) the newline character at any end of the output (only valid in the parameter string under the control of the%b format designator), and any characters left in the argument, any subsequent arguments, and any characters left in the format string are ignored
\f Page Change (formfeed)
\ n Line break
\ r Enter (carriage return)
\ t Horizontal tab
\v Vertical tab
\\ A literal backslash character
\ddd A character that represents an octal value of 1 to 3 digits. Valid only in format strings
\0ddd Represents octal value characters from 1 to 3 bits

Shell Tutorial's printf command

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.