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-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "how to use printf commands in Linux", so the editor summarizes the following contents, detailed contents, 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 "how to use printf commands in Linux" article.

The printf command mimics the printf () function in the C language. The main function is to output text, according to the format we specify. There is also a command echo that outputs text, and when you output text, echo wraps lines. The printf command does not wrap the output text.

Syntax format: # printf format parameter

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 character\ "- escaped double quotes\-escaped backslash\ b-backspace character\ n-newline character\ r-carriage return\ t-horizontal tab character\ v-Vertical tab%-Type conversion character commonly used in a single% symbol

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 instance 1

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" 100100 100Decimal: 100Hex: 64Octal: 144Instanc II

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 instance 3

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", "Xiaoming", "female", "19", "Little Blue", "male", "18" name, gender age, 18 Xiaoming male, 19 Xiaolan male, 18 Xiaolan male

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.shanglang binbinhprintf "\ e [1set30th 47m% 10s%-10s\ e [0m\ n"name", "age" printf "\ e [36th 47m% helicopter 10s%-8d\ e [0m\ n", "Xiaoming"18" printf "\ e [31transfer47m% copyright 10s%-8d\ e [0m\ n"Xiao Hong"19" printf "\ e [34" 47m%-10s%-8d\ e [0m\ n "" Xiaolan "" 19 "above is about the content of this article" how to use printf commands in Linux ". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please 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