What does printf mean in linux?

WBOY
WBOYOriginal
2022-03-15 14:54:013748browse

In Linux, printf means formatted output. This command can better control the output format. Its main function is to output text according to the specified format. This command will not wrap the output text. The syntax is "printf format parameter".

What does printf mean in linux?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

What does printf mean in Linux

When writing shell scripts, we use echo to print to standard output. echo is a simple command, but its function is limited. If you want to have better control over the output format, you can use the printf command.

The syntax format of printf:

# printf 格式 参数

The following is an example:

[root@localhost ~]# printf "姓名:%s\n身高:%dcm\n体重:%dkg\n" "小明" "180" "75"
姓名:小明
身高:180cm
体重:75kg

What does printf mean in linux?

"Name: %s\nHeight: �m \nWeight: %dkg\n" is the format, "Xiao Ming" "180" "75" is the parameter. The format includes %s and %d, which are format conversion characters. The parameter corresponding to %d must be a decimal number. The parameter corresponding to %s must be a string. It also includes three \n newline characters. The printf command does not add the OK symbol by default and needs to be added manually.

Commonly used escape characters

  • ##\" - Escaped double quotes

  • \\ - Escaped backslash

  • ##\b

    - Backspace character

  • \n

    - Line feed character

  • \r

    - Carriage return character

  • \t

    - Horizontal tab character

  • \v

    - Vertical tab character

  • %%

    - A single % symbol

  • Commonly used type conversion characters

The type conversion specifier is a character used to specify How to interpret the corresponding parameters. This character is required. The following is a list showing all type conversions and their effects:

  • %d

    - Print the parameters as Decimal integer

  • %f

    - Print the parameter as a floating point number

  • %s

    - Will Print parameters as string

  • %x

    - Print parameters as hexadecimal integers

  • % o

    - Print parameters as octal integers

  • Example 1

This example uses %d,%x,%o to convert the parameters provided later into decimal , hexadecimal, octal.

[root@localhost ~]# printf "Decimal: %d\nHex: %x\nOctal: %o\n" 100 100 100
Decimal: 100
Hex: 64
Octal: 144

Example 2

The following example uses %.2f, where .2 means that the parameter retains two decimal places, and f prints the parameter as a floating point number.

[root@localhost ~]# printf "%.2f\n" 3.1415926
3.14

Related recommendations: "

Linux Video Tutorial

"

The above is the detailed content of What does printf mean in linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn