Linux Bash Shell Printf命令示例

Linux provides different commands to print given string or data into the terminla or specified place. printf is one of them which is similar to the C programming language printf()  function. Actually it is cloned from C printf function which provides similar features to write given string to the terminal in a structured manner.

Linux提供了不同的命令来将给定的字符串或数据打印到terminla或指定的位置。 printf是其中之一,类似于C编程语言printf()函数。 实际上,它是从C printf函数克隆的,该函数提供类似的功能,以结构化方式将给定的字符串写入终端。

帮帮我 (Help)

We will start by printing help information about printf . And as we will examine most of the options provided by printf in the next part of the tutorial.

我们将从打印有关printf帮助信息开始。 正如我们将在本教程的下一部分中研究printf提供的大多数选项一样。

$ man printf
Help
Help
帮帮我

只需打印(Simply Print)

We will start by simply printing given string. We will provide the string we want to print in a double quote. In this example we will print hi poftut .

我们将从简单地打印给定的字符串开始。 我们将在双引号中提供要打印的字符串。 在此示例中,我们将打印hi poftut

$ printf "hi poftut"
Simply Print
Simply Print
只需打印

字串格式(String Format)

We can use %s  in order to provide external strings to the given string. External strings will be added to the %s place. In this example we will provide poftut  as external string.

我们可以使用%s来为给定的字符串提供外部字符串。 外部字符串将添加到%s位置。 在此示例中,我们将poftut作为外部字符串提供。

$ printf "hi %s" "poftut"
String Format
String Format
字串格式

多字符串格式(Multiple String Format)

We can also provide multiple external string which will cause multiple print like a loop.

我们还可以提供多个外部字符串,这将导致多次打印,例如循环。

$ printf "hi %s" "poftut" "ismail"
Multiple String Format
Multiple String Format
多字符串格式

双引号(Double Quote)

Because we use double quote in shell we need to have some special mechanism in order to print double quotes in string. We will use \" to print double quote properly.

因为我们在shell中使用双引号,所以我们需要某种特殊的机制才能在字符串中打印双引号。 我们将使用\"正确打印双引号。

$ printf “hi \”ismail\” ”

$ printf“ hi \” ismail \””

Double Quote
Double Quote
双引号

反斜杠(Backslash)

As backslash is used as a helper we can use \\ as backslash like below.

由于反斜杠用作帮助程序,因此可以使用\\作为反斜杠,如下所示。

$ printf “hi \ismail ”

$ printf“ hi \ ismail”

Backslash
Backslash
反斜杠

警报(Alert)

If given terminal supports sound we can play a ping sound with \a which is named alert.

如果给定的终端支持声音,我们可以使用\a播放ping声音,命名为alert。

$ printf "hi \a ismail "

退格键 (Backspace)

Backspace is used to delete previously given string. We will use \b to specify backspace. In this example hi  will be deleted with two backspace.

退格键用于删除先前给定的字符串。 我们将使用\b来指定退格键。 在此示例中, hi将被删除,并带有两个退格键。

$ printf "hi\b\bismail"
Backspace
Backspace
退格键

没有进一步的输出(No Further Output)

We can stop output at the given position of the string we will use \c which is shortcut for cancel. In this example we will cut output after hi.

我们可以在字符串的给定位置停止输出,我们将使用\c ,这是取消的快捷方式。 在此示例中,我们将在hi之后剪切输出。

$ printf "hi \c ismail"

逃逸 (Escape)

We can use \e for a escape secuence.

我们可以使用\e来逃避安全。

$ printf "hi \e ismail"

新队 (New Line)

As we have all ready see that after the end of the string there is no new line which will cause terminal start to print to the same line of the string. We can use \n as new line which will start new line.

正如我们已经准备就绪的那样,看到字符串的结尾之后没有新行,这将导致终端开始打印到字符串的同一行。 我们可以使用\n作为新行,这将开始新行。

$ printf "hi \nismail \n"
New Line
New Line
新队

回车(Carriage Return)

We can use \r for carriage return key.

我们可以使用\r作为回车键。

$ printf "hi \rismail"

水平制表符 (Horizontal Tab)

Tabs provides some space between given position. Horizontal tab is used to place spaces in horizontal manner.

制表符在给定位置之间提供了一些空间。 “水平”选项卡用于以水平方式放置空间。

$ printf "hi \tismail"
Horizontal Tab
Horizontal Tab
水平制表符

垂直标签(Vertical Tab)

Vertical tab will place a tab in a vertical manner which will put given string to the next line.

垂直制表符将以垂直方式放置一个制表符,这会将给定的字符串放在下一行。

$ printf "hi \vismail"
Vertical Tab
Vertical Tab
垂直标签

打印十进制格式(Print Decimal Format)

We can print given hex or octal value in decimal format with %d . In this example we will print hex number 0xF in decimal.

我们可以使用%d以十进制格式打印给定的十六进制或八进制值。 在此示例中,我们将以十进制打印十六进制数字0xF

$ printf "Number is %d \n" 0xF
Print Decimal Format
Print Decimal Format
打印十进制格式

打印十六进制格式(Print Hexadecimal Format)

We can also print in Hex or Hexadecimal format with %X .We will also add 0x for prefix.

我们也可以使用%X以十六进制或十六进制格式打印。我们还将在前缀上添加0x

$ printf "Number is 0x%X \n" 15
Print Hexadecimal Format
Print Hexadecimal Format
打印十六进制格式

打印八进制格式(Print Octal Format)

Octal format is used in 8 numbering system. We can use %o to print in octal format like below.

在8编号系统中使用八进制格式。 我们可以使用%o以八进制格式打印,如下所示。

$ printf "Number is %o \n" 15
Print Octal Format
Print Octal Format
打印八进制格式
LEARN MORE  Python Type Function with Examples
了解更多带有示例的Python类型函数

翻译自: https://www.poftut.com/linux-bash-shell-printf-command-examples/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值