Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use the printf command in Shell

Shulou Source: shulou.com Published: 2022-06-01 05:09:26 10月27日 Update

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!

Tags: Character format parameter quotation mark output command content string article script weight name instance width decimal gender more space result powerful Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Microsoft Xiaomi Shulou Information Shulou Tech Info