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 common command printf in Linux

2025-02-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use the Linux command printf. I hope you will get something after reading this article. Let's discuss it together.

Linux commonly used commands printf commands mimic the printf () program in the C library (library). Printf is defined by the POSIX standard, so scripts using printf are more portable than using echo.

Syntax format: # printf format parameter instance [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

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" 100100 100 Decimal: 100 Hex: 64 Octal: 144instance 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.1415926 3.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", "Xiao Hong", "female", "19", "Little Blue", "male", "18" name, gender age, 18 Xiaoming male, 19 Xiaoxing 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/bash printf "\ 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 "" Little Blue "" 19 "

After reading this article, I believe you have a certain understanding of "how to use Linux commands printf". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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