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 Shell

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Editor to share with you how to use the printf command in Shell, 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!

Printf uses parameters separated by reference text or spaces, and you can use formatted strings in printf, and you can also specify the width of the string, left and right alignment, and so on. The default printf does not automatically add newline characters like echo, we can add\ n manually.

The syntax of the printf command:

Printf format-string [arguments...]

Parameter description:

Format-string: controlling strings for formatting

Arguments: is the parameter list.

Examples are as follows:

$echo "Hello, Shell" Hello, Shell$ printf "Hello, Shell\ n" Hello, Shell$

Next, I'll use a script to demonstrate the power of printf:

#! / bin/bashprintf "%-10s%-8s%-4s\ n" name, gender and weight kgprintf "%-10s%-8s%-4.2f\ n" Guo Jing male 66.1234printf "%-10s%-8s%-4.2f\ n" Yang Guo male 48.6543printf "%-10s%-8s%-4.2f\ n" Guo Fu female 47.9876 "

Execute the script, and the output is as follows:

Name, sex, weight kg Guo Jing male 66.12 Yang Guo male 48.65 Guo Fu female 47.99

S, c, d, f are all format substitutes

%-10s means a width of 10 characters (- for left alignment, none for right alignment), any character will be displayed in a 10 character width character, if it is insufficient, it will be automatically filled with spaces, and the excess will also display all the content.

%-4.2f refers to formatted as a decimal, where .2 means to retain 2 decimal places.

More examples:

#! / bin/bash# format-string is double quotation marks printf "% d% s\ n" 1 "abc" # single quotation marks have the same effect as double quotation marks printf'd% s\ n'1 "abc" # can output without quotation marks printf% s abcdef# format specifies only one parameter, but the extra parameters will still be output according to this format Format-string is reused printf% s abc defprintf "% s\ n" abc defprintf "% s% s% s\ n" abc def g h i j# if there is no arguments, then% s is replaced with NULL and% d with 0 instead of printf "% s and% d\ n"

Execute the script, and the output is as follows:

1 abc1 abcabcdefabcdefabcdefa bcde fg h ijand 0 and above are all the contents of this article entitled "how to use printf commands in Shell". 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

Servers

Wechat

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

12
Report