Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the printf command in Linux

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use the printf command in Linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Grammatical 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 quotes\-escaped backslash\ b-backspace character\ n-newline character\ r-carriage return\ t-horizontal tab\ 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 parameter as decimal integer% f-print parameter as floating point number% s-print parameter as string% x-print parameter as hexadecimal integer% o-print parameter as octal integer

Real example

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" 3.14159263.14

Example three

The horizontal tab character\ t is used in the following example. The parameters given in the example exceed the number of conversion characters given in the format, and the extra parameters will still be output according to the format.

[root@localhost] # printf "% s\ t% s\ t% s\ n"name", "gender", "age", "Xiaoming", "male", "Xiao Hong", "female", "19", "Little Blue", "male", "18" name, gender age, 18 Xiaoming male, 19 Xiaolong female, 18 Xiaolan male.

Example 4

In the instance,%-10s,%-8d, where-indicates left alignment, and right alignment by default. 10 and 8 represent a width of 10 or 8 characters. \ e [1BO 30th 47m. \ e [0m means to use bold type, black font, white background, followed by\ e [0Magnem] to indicate reset.

[root@localhost ~] # cat p.sh #! / bin/bashprintf "\ e [1Trading 30th 47m% Mutual 10s%-10s\ e [0m\ n", "name", "age" printf "\ e [36Trans47m% Musical 10s%-8d\ e [0m\ n"Xiaoming"18" printf "\ e [31X 47m% Mutual 10s%-8d\ e [0m\ n"Xiao Hong"19" printf "\ e [34] 47m%-10s%-8d\ e [0m\ n "," Xiaolan "and" 19 "are all the contents of this article" how to use printf commands in Linux ". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report