Linux Shell Scripting Tutorial Series (8) Detailed Explanation of Shell Printf Command

This article is the (eighth) part of the Linux Shell Scripting Series . For more shell tutorials, please see: Linux Shell Scripting Series Tutorials

In the last article: Linux Shell Series Tutorials (7) Shell Output In this article, I have given a brief introduction to the Shell printf command. This article will give you a detailed introduction to the printf command in Shell.

 

1. Shell printf command syntax

printf  format-string  [arguments...]

 

format-string is a string that describes the format specification and is used to describe the arrangement of the output. It is best to add quotation marks to this string. This string contains literal characters and format declarations, with special placeholders in format declarations describing how to display the corresponding parameters.

arguments is a list of arguments corresponding to the format declaration, such as a series of strings or variable values.

A format declaration consists of two parts: a percent sign (%) and an indicator.

The two most commonly used format specifiers are %s for strings and %d for decimal integers.

In the format string, normal characters are displayed literally. Escape sequences, like echo, are interpreted and then output as the corresponding characters. A format declaration begins with a % sign and ends with one of a defined set of letters that control the output of the corresponding parameter.

 

2. Shell printf command escape sequence

The escape sequences for the Shell printf command are shown in the following table

sequence  
\a Warning character, usually the ASCII BEL character
\b back
\c Any trailing newline characters in the output are not displayed, and any characters left in parameters, any subsequent parameters, and any characters left in the format string are ignored.
\f form feed
\n newline
\r Enter
\t horizontal tab
\v vertical tab
\\ backslash character

 

3. Shell printf command format indicator

The format specifiers for the Shell printf command are shown in the following table

symbol illustrate
%c ASCII character. Displays the first character of the corresponding parameter
%d,%i decimal integer
%E floating point format ([-d].precisionE[+-dd])
%e floating point format ([-d].precisione [+-dd])
%g %e or %f conversion, whichever is shorter, remove trailing zeros
%G %E or %f conversion, whichever is shorter, remove trailing zeros
%s string
% u unsigned decimal value
%x Unsigned hexadecimal. Use a to f for 10 to 15
%% literal %
%X Unsigned hexadecimal. Use A to F for 10 to 15

 

 

Fourth, Shell printf command precision format indicator

The precision format indicator in the Shell printf command is shown in the following table

symbol meaning
%d,%i,%o,%u,%x,%X The minimum number of digits to print. When the value has fewer digits than this, it will be padded with leading zeros. The default precision is 1
%and and The minimum number of digits to be printed. When the number of digits in the value is less than this number, zeros will be added after the decimal point. The default is the precision of 6. If the precision is 0, the number of digits to the right of the decimal point will not be displayed.
%f digits to the right of the decimal point
%g,%G maximum number of significant digits
%s maximum number of characters to print

 

Example of use:

printf "%.5d\n" 15
#output: 00015

printf "%.10s\n" "a very long string"
#Output: a very lon

printf "%.2f\n" 123.4567
# output: 123.46

 

 

Five, Shell printf command some identifiers

Some common identifiers of Shell printf command are shown in the following table

character illustrate
Aligns formatted values ​​in a field to the left
space Prepend a space to positive values ​​and a minus sign to negative values
+ Always place a positive or negative sign before the value, even if it is a positive value
# Choose one of the following forms: %o has a leading o;
%x and %X have leading 0x and 0X respectively;
%e, %E and %f always have a decimal point in the result;
%g and %G for no trailing zeros.
0 Pads the output with zeros, not blanks. This only happens if the field width is larger than the converted

 

Example of use:

$printf "%-20s%-15s%10.2f\n" "Shan" "zhang" 35       
# 输出:Shan                zhang               35.00
# %-20s表示一个左对齐、宽度为20个字符字符串格式,不足20个字符,右侧补充相应数量的空格符。
# %-15s表示一个左对齐、宽度为15个字符字符串格式。
# %10.2f表示右对齐、10个字符长度的浮点数,其中一个是小数点,小数点后面保留两位。

$printf "|%10s|\n" hello
# 输出:|     hello|
# %10s表示右对齐,宽度为10的字符串,如不足是个字符串,左侧补充相应数量的空格数.

$printf "|%-10s|\n" hello
# 输出:|hello     |
# 和案例二比较一下

$printf "%x %#x\n" 15 15
# 输出:f 0xf
# 如果#标志和%x,%X搭配使用,在输出十六进制数字时,前面回家0x或者0X前缀.
# 使用标志符的作用主要是为了动态的指定宽度和精度.

 

六、Shell printf命令的一些综合示例

下面给大家总结了一些比较综合的例子,希望能够帮助大家学习Shell printf命令。

#字符串向左向右对齐:
$printf "|%-10s| |%10s|\n" hello world
#输出|hello     | |     world|

#空白标志:
$printf "|% d| |% d|\n" 15 -15                 
#输出:| 15| |-15|

#+标志:
$printf "|%+d| |%+d|\n" 15 -15  
#输出:|+15| |-15|

#标志:
$printf "%x || %#X\n" 15 15
#输出:f || 0XF

#0标志:
$printf "%05d\n" 15
#输出:00015

 

对于转换指示符%b、%c与%s而言,相对应的参数都应为字符串。否则,他们会被解释为C语言的数字常数(开头的0位八进制,以及开头的0x与0X为十六进制)。

更进一步说,如果参数的第一个字符为单引号或双引号,则对应的数值是字符串的第二个字符的ASCII值,比如:

命令:printf “%s is %d \n” a “‘a”

输出:a is 97

当参数多于格式指示符时,格式指示符会根据需要再利用。

这种做法在参数列表长度未知时非常方便。

 

例如来自通配符表达式,如果留在格式字符串里剩下的指示符比参数多时,如果是数值转换,则遗漏的值会被看做是零。

 

但如果是字符串转换,则被视为空字符串(虽然可以这么用,但比较好的方式应该是一一对应关系,即提供的参数数目和格式字符串数目相同)。

如果printf无法进行格式的转换,便返回一个非零的退出状态。

 

好了,对于Shell printf命令的详解就先进行到这里,printf是一个非常强大的命令,希望大家平时能够多多练习,争取熟练掌握。

更多Shell教程请看:Linux Shell脚本系列教程

 

原文:Linux Shell系列教程之(八)Shell printf命令详解

本文转自:Linux Shell脚本入门教程系列之(八)Shell printf命令详解

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326682517&siteId=291194637