Linux Command Learning Manual-printf command

Source: Internet
Author: User

Category: LINUX

Reference: http://sns.linuxpk.com/space-566-do-blog-id-15819.html

printf FORMAT [ARGUMENT] ...

printf OPTION

function

Format and print the data.

For example

* Print integers and strings:

$printf ' The integer is:%d\nthe string is:%s\n ' 3 "test"

After input, the output is as follows:

The integer is:3

The string is:test

Here, use single-quote double quotation marks, and note that you will end up with a carriage return, otherwise the next prompt line and output run to a line.

* The case of printing a parameter without quotation marks:

$printf%s Test

After input, the output is as follows:

[Email protected]$

As can be seen here, test is the character to be printed, [email protected]$ is my machine command prompt symbol, both in one line.

* Case where multiple parameters are printed without quotation marks:

$printf%s First Second

After input, the output is as follows:

[Email protected]$

* The format specifies only one, but one extra parameter:

$printf "%s\n" first second

After input, the output is as follows:

First

Second

Note the carriage return here, the string can still be output.

* The specified format is less than the parameter:

$printf "%s%s%s%s\n" a b c D e F g h i j k l m N

After input, the output is as follows:

A B c D

E F g H

I j k L

M n

Here, the condition is preceded by specifying a format for the case extension.

* Similar to the output of echo-n, does not wrap the line:

$my _var= "abc123"

$printf $my _var

After input, the output is as follows:

[Email protected]$

If the echo output wraps automatically, there is no line wrap, which is the same as echo-n.

* Output similar to echo, Wrap line:

$printf "$my _var \ n"

After input, the output is as follows:

abc123

or $printf ' $my _var \ n '

After input, the output is as follows:

$my _var

Here, these two commands are similar to echo, and it's important to note that you can't do the following:

$printf '%s \ n ' $var

Because, a variable cannot be so output because the argument of the printf command is a string.

* If the parameter is not written, then replace with null or:

$printf "%s and%d \ n"

After input, the output is as follows:

and 0

Description

This function is similar to printf in C, specifying the format and then writing the variables.

Different points, the shell command of printf

1. There is no required parentheses in the C language function;

2. In general, when specifying format, use both single and double quotation marks;

3. In some cases, no quotation marks are allowed, but the responsible format cannot be processed;

4. In some cases, format can be reused, and all parameters can be converted;

5. Output variable parameters using space or tab segmentation, without commas;

6. You can replace echo in some way.

Other

Linux Command Learning Manual-printf command (GO)

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.