Get the App
SLTechnology News&Howtos  ›  Servers  › 

What does printf mean in linux

Shulou Source: shulou.com Published: 2022-05-31 11:04:02 10月03日 Update

Most people do not understand the knowledge points of this article "what does printf in linux refer to?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what printf refers to in linux" article.

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

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 functionality is limited. For better control over the output format, you can use the printf command.

The syntax format of printf:

# printf format parameters

Here is an example:

[root@localhost ~] # printf "name:% s\ nheight:% dcm\ nweight:% dkg\ n" Xiaoming "180," 75" name: Xiaoming height: 180cm weight: 75kg

"name:% s\ nheight:% dcm\ nweight:% dkg\ n" is the format, "Xiaoming"180"75" is the parameter. The format contains% s,% d is a format conversion character, and the parameter corresponding to% d must be a decimal number. The parameter for s must be a string. Three newline characters are also included. The printf command does not add line return symbols by default, but needs to be added manually.

Commonly used escape characters

\ "- escaped double quotation marks

\-backslash after escape

\ b-backspace character

\ n-newline character

\ r-carriage return

\ t-horizontal tab character

\ v-Vertical Tab

%%-single% symbol

Commonly used type conversion characters

The type conversion specifier is a character that specifies how to interpret the corresponding parameter, which must be added. The following is a list of all types of conversions and their functions:

% d-print parameters as decimal integers

% f-print parameters as floating-point numbers

% s-print parameters as strings

% x-prints parameters as hexadecimal integers

% o-print parameters as octal integers

Example one

This example uses% dforce% xmy% o to convert the parameters provided later into decimal, hexadecimal, and octal.

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

Example two

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" above 3.14159263.14 is about "what is printf in linux"? I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Parameters format output content commands characters weight examples decimal name integer type height escape Xiaoming decimal function octal tab hexadecimal Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MariaDB Shulou Information macOS Huawei