Use the printf command shell output of linux
Editor to share with you the use of linux printf command shell output, I believe that most people do not know much, so share this article for you to learn, I hope you will learn a lot after reading this article, let's go to learn the method!
The printf command mimics the printf () program in the C library (library).
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.
Syntax format: printf [format control string] [parameter]
Common parameters:
\ a warning character, usually ASCII's BEL character\ nnewline\ r carriage return\ 0ddd for 1 to 3 octal value characters\ ddd for 1 to 3 octal values. Valid only in format strings
Reference example
String output:
[root@linuxcool ~] # printf "Hello, Shell\ n" Hello, Shell
Next, I'll use a script to demonstrate the power of printf:
[root@linuxcool] # printf "%-10s%-8s%-4s\ n" name, gender and weight kgprintf "%-10s%-8s%-4.2f\ n" Guo Jing male 66.1234 printf "%-10s%-8s%-4.2f\ n" Yang Guo Nan 48.6543 printf "%-10s%-8s%-4.2f\ n" Guo Fu female 47.9876 "
Execute the script, and the output is as follows:
[root@linuxcool] # name, sex and 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.
The format control string is in double quotes:
[root@linuxcool ~] # printf "% d% s\ n" 1 "abc"
The format control string is in single quotes:
[root@linuxcool ~] # printf'% d% s\ n'1 "abc"
You can output without quotation marks:
[root@linuxcool ~] # printf% s abcdef above is all the content output using linux's printf command 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!