Getting Started with Shell Scripting_8

In Getting Started 6, we learned the shell's echo command, and Getting Started 7 learned another Shell output command, printf.

The printf command mimics the printf() program in the C library.

printf is defined by the POSIX standard, so scripts using printf are more portable than using echo.

Remarks: POSIX stands for Portable Operating System Interface of UNIX (abbreviated as  POSIX  );

printf uses quoted text or space-separated parameters, and format strings can be used in printf outside, and the width of the string, left and right alignment, etc. can also be specified. By default printf does not automatically add newlines like echo, we can manually add \n .

Syntax of the printf command:

printf  format-string  [arguments...]

Parameter Description:

  • format-string:  is the format control string.
  • arguments:  is the parameter list.

Examples are as follows:

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

Next, let me use a script to embody the power of printf:

#!/bin/bash
# author:ethan
 
printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg  
printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234 
printf "%-10s %-8s %-4.2f\n" 杨过 男 48.6543 
printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876 

执行脚本,输出结果如下所示:
姓名     性别   体重kg
郭靖     男      66.12
杨过     男      48.65
郭芙     女      47.99

%s %c %d %f are format surrogates

%- 10s refers to a width of 10 characters ( - means left-aligned, no means right-aligned ), any character will be displayed within 10 characters wide characters, if it is insufficient, it will be automatically filled with spaces, if it exceeds, the content will also be displayed. All displayed .

%-4.2f refers to formatting as a decimal, where .2 refers to 2 decimal places .

More examples:

#!/bin/bash
# author:ethan
 
# format-string为双引号
printf "%d %s\n" 1 "abc"

# 单引号与双引号效果一样 
printf '%d %s\n' 1 "abc" 

# 没有引号也可以输出
printf %s abcdef

# 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用; ***注意***
printf %s abc def

printf "%s\n" abc def

printf "%s %s %s\n" a b c d e f g h i j

# 如果没有 arguments,那么 %s 用NULL代替,%d 用 0 代替 ****注意****
printf "%s and %d \n" 

执行脚本,输出结果如下所示:
1 abc
1 abc
abcdefabcdefabc
def
a b c
d e f
g h i
j  
 and 0

Escape sequences for printf

sequence illustrate
\a Warning character, usually the ASCII BEL character
\b back
\c Suppresses (does not display) any trailing newline characters in the output (valid only in parameter strings under the control of the %b format specifier), and any characters left in the parameter, any subsequent parameters, and any remaining characters in the parameter characters in the format string, are ignored
\f formfeed
\n newline
\r Carriage return
\t horizontal tab
\v vertical tab
\\ a literal backslash character
\ddd A character representing a 1- to 3-digit octal value. only valid in format strings
\ 0ddd A character representing an octal value of 1 to 3 digits

example

$ printf "a string, no processing:<%s>\n" "A\nB"
a string, no processing:<A\nB>

$ printf "a string, no processing:<%b>\n" "A\nB"    ***没完全理解***
a string, no processing:<A
B>

$ printf "www.runoob.com \a"
www.runoob.com $                  #不换行

Remark:

%d %s %c %f Format substitution details:

d: Decimal decimal integer  -- the corresponding position parameter must be a decimal integer, otherwise an error will be reported!

s: String string  -- the corresponding positional parameter must be a string or character type, otherwise an error will be reported!

c: Char character  -- the corresponding positional parameter must be a string or character type, otherwise an error will be reported!

f: Float  -- the corresponding position parameter must be a number, otherwise an error will be reported!

Such as: the last parameter is "def", %c automatically intercepts the first character of the string as the result output.

$  printf "%d %s %c\n" 1 "abc" "def"
1 abc d

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325473723&siteId=291194637