Home >Operation and Maintenance >Linux Operation and Maintenance >What does printf mean in linux?
In Linux, printf means formatted output. This command can better control the output format. Its main function is to output text according to the specified format. This command will not wrap the output text. The syntax is "printf format parameter".
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
When writing shell scripts, we use echo to print to standard output. echo is a simple command, but its function is limited. If you want to have better control over the output format, you can use the printf command.
The syntax format of printf:
# printf 格式 参数
The following is an example:
[root@localhost ~]# printf "姓名:%s\n身高:%dcm\n体重:%dkg\n" "小明" "180" "75" 姓名:小明 身高:180cm 体重:75kg
"Name: %s\nHeight: �m \nWeight: %dkg\n" is the format, "Xiao Ming" "180" "75" is the parameter. The format includes %s and %d, which are format conversion characters. The parameter corresponding to %d must be a decimal number. The parameter corresponding to %s must be a string. It also includes three \n newline characters. The printf command does not add the OK symbol by default and needs to be added manually.
Commonly used escape characters
##\" - Escaped double quotes
\\ - Escaped backslash
- Backspace character
- Line feed character
- Carriage return character
- Horizontal tab character
- Vertical tab character
- A single % symbol
The type conversion specifier is a character used to specify How to interpret the corresponding parameters. This character is required. The following is a list showing all type conversions and their effects:
- Print the parameters as Decimal integer
- Print the parameter as a floating point number
- Will Print parameters as string
- Print parameters as hexadecimal integers
- Print parameters as octal integers
This example uses %d,%x,%o to convert the parameters provided later into decimal , hexadecimal, octal.
[root@localhost ~]# printf "Decimal: %d\nHex: %x\nOctal: %o\n" 100 100 100 Decimal: 100 Hex: 64 Octal: 144
Example 2
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
Related recommendations: "
Linux Video TutorialThe above is the detailed content of What does printf mean in linux?. For more information, please follow other related articles on the PHP Chinese website!