Linux bash shell Learning (18): string I/O -- ECHO and printf

Source: Internet
Author: User

This article is the seventh chapter of "Learning the bash Shell" 3rd Edition, "Input/Output and Command-Line Processing.

Echo

Echo is a very common shell command. The parameters are as follows:

  • -E: parses backslash-escaped, a backslash (/n),/t, and other characters, rather than two characters.
  • -E: Disable the parsing of backslash characters./N is used as two characters, which is the default mode.
  • -N: Delete the last line feed.

Backslash-escaped has the following parameters. Note that you must select-e to apply the parameters.


/

: Alert bell sound (alert or CTRL-G (Bell ))

/B

: Return (backspace or CTRL-H)

/C

: Delete the last character and the last line break (omit final newline)

/E

: Delete a character (Escape Character (same as/E ))

/E

: Same as above (Escape Character)

/F

: Page feed, clear in some reality, some will wrap (formfeed or CTRL-L)

/N

: Newline (not at end of command) or CTRL-J)

/R

: Starting from the line header, not the line break, still in the line (Return (enter) or CTRL-M)

/T

: Tab key (tab or CTRL-I)

/V

: Vertical tab, like/F, display different machines are different, usually cause line feed vertical tab or CTRL-K

/N

: When/65 is used in cygwin, 'A' cannot be correctly displayed, but either of the following two methods can be displayed. ASCII character with octal (base-8) value n, where n is 1 to 3 digits

/0Nnn

: Represents a character with an octal value, for example,/0101, or 65, representing the character 'a' (the eight-bit character whose value is the octal (base-8) value NNN where NNN is 1 to 3 digits)

/XHH

: Represents a character with a hexadecimal value, for example,/x41, or 65, representing the character 'a' the eight-bit character whose value is the hexadecimal (base-16) value HH (one or two digits)

//

: '/' Single backslash

The reciprocal of 2nd-4 represents a number of characters related to the device. It is usually used for complex IO operations, such as cursor control and special graphical symbols.

Printf

Linux provides a more powerful and flexible print command printf. The usage of printf is similar to that of C language. Unlike echo, it does not automatically add line breaks at the end and needs to be written into the command. For example, printf "Hello, world/n ". The Command Format of printf is as follows:

Printf format-string [arguments]

Example: printf "% s, % s/n" hello world. If the format requires more parameters than the following one, the excess part is 0 or null. The format is as follows.

% C: ASCII character. If the parameter is a string, the first character is printed.

% D: 10-digit integer

% I: Same as % d

% E: Floating Point format ([-] D. Precision [+-] dd)

% E: Floating Point format ([-] D. Precision E [+-] dd)

% F: Floating Point format ([-] DDD. Precision)

% G: % E or % F conversion. If the end is 0, delete them.

% G: % E or % F conversion. If the end is 0, delete them.

% O: octal

% S: String

% U: non-zero positive integer

% X: hexadecimal

% X: a non-zero positive number in hexadecimal notation with a A-F representing 10-15

%: The character "%"

If you want to limit the output width, the format is % flags width. precision format-specifier, width is an integer, right alignment. If left alignment is required, add "-" to the front. For example, "%-20 s" indicates alignment from the left, the width is 20. If the string length is less than 20, fill it with spaces. Precision provides rounding in floating point values. For example, % 5.6G. The length is 5 and the precision is 6. Precision is optional. Length and Precision values can be specified in parameters, such as printf "% *. * G/n" 5 6 $ myvalue. Length refers to the length of Characters in the display, which is synonymous with the character length. If the length is less than the actual length, for example, the actual length is greater or the required precision is greater, the display is based on the actual length.

% D, % I, % O, % u, % x, % x: the minimum number displayed. If the number displayed is less than the requirement, add 0. The default value is 1.

% E, % E: The smallest number displayed. If the number displayed is less than the requirement, add 0 after the decimal point. The default value is 10. If the precision is 0, the decimal point is hidden.

% F: The precision here indicates the number of digits after the decimal point.

% G, % G: The precision here, indicating the maximum number of valid numbers.

% S: The precision here, indicating the maximum number of characters.

Previously, we introduced "-" as left alignment. The following describes these special symbols.

-: Left alignment

Space: Add a space before a positive number, and add a symbol before a negative number, for example, 12.12, | % F |, display | 12.120000 |. Note that a space is left in front. For example,-12.12, | % F |, displayed |-12.120000 |

+: Numbers are given plus or minus signs.

#: Another format is provided:

  • % O is an integer in octal format, while % # O is preceded by zero, indicating that it is octal, for example, 12, indicating 014
  • % X or % x displays integers in hexadecimal notation, while % # X or % # X is represented by 0x or 0x before the hexadecimal integer, for example, 12, show 0xc
  • % # E, % # E, % # F, will only be displayed in decimal format
  • % # G, % # G, do not delete the last irrelevant completion 0, for example, 12.1200, will be displayed all, instead of 12.12.

0: for the number, no space is used, and 0 is used as the complement.

This section describes two special character display formats: % B and % q.

% B: special characters of the parsed string, including/n. For example, printf "% s/n" 'Hello/nworld' displays Hello/nworld. To use/N as a line break, you must usePrintf "% B/n" 'Hello/nworld'

.

% Q: printf "% Q/N" "greetings to the world" is displayed as greetings/to/The/World, which can be used as shell input.

Related Links: My articles on Linux operations

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.