php 命令行打印换行符
Surprisingly, getting computers to give humans readable output is no easy feat. With the introduction of standard streams and specifically standard output, programs gained a way to talk to each other using plain text streams. But humanizing and displaying stdout is another matter. Technology throughout the computing age has tried to solve this problem, from the use of ASCII characters in video computer displays to modern shell commands like echo
and printf
.
令人惊讶的是,让计算机为人类提供可读的输出并非易事。 通过引入标准流 ,特别是标准输出,程序获得了一种使用纯文本流进行对话的方法。 但是人性化和显示标准输出是另一回事。 从在视频计算机显示器中使用ASCII字符到现代的shell命令(例如echo
和printf
,整个计算时代的技术都试图解决这个问题。
These advancements have not been seamless. The job of printing output to a terminal is fraught with quirks for programmers to navigate, as exemplified by the deceptively nontrivial task of expanding an escape sequence to print newlines. The expansion of the placeholder \n
can be accomplished in a multitude of ways, each with its own unique history and complications.
这些进步并不是无缝的。 将输出打印到终端的工作充满了让程序员进行导航的怪癖,例如通过扩大转义序列以打印换行符的看似不平凡的任务就可以说明这一点。 占位符\n
的扩展可以通过多种方式完成,每种方式都有其独特的历史和复杂性。
使用echo
(Using echo
)
From its appearance in Multics to its modern-day Unix-like system ubiquity, echo
remains a familiar tool for getting your terminal to say “Hello world!” Unfortunately, inconsistent implementations across operating systems make its usage tricky. Where echo
on some systems will automatically expand escape sequences, others require a -e
option to do the same:
从在Multics中出现到现代的类Unix系统无处不在, echo
仍然是一种熟悉的工具,可以使您的终端说“ Hello world!”。 不幸的是,跨操作系统的不一致实现使它的使用变得棘手。 在某些系统上的echo
会自动扩展转义序列的情况下, 其他系统则需要-e
选项来执行相同的操作:
echo "the study of European nerves is \neurology"
# the study of European nerves is \neurology
echo -e "the study of European nerves is \neurology"
# the study of European nerves is
# eurology
Because of these inconsistencies in implementations, echo
is considered non-portable. Additionally, its usage in conjunction with user input is relatively easy to corrupt through shell injection attack using command substitutions.
由于实现中的这些不一致, echo
被认为是不可移植的。 另外,它与用户输入的结合使用相对容易通过使用命令替换的shell注入攻击来破坏。
In modern systems, it is retained only to provide compatibility with the many programs that still use it. The POSIX specification recommends the use of printf
in new programs.
在现代系统中,保留它只是为了提供与仍在使用它的许多程序的兼容性。 POSIX规范建议在新程序中使用printf
。
使用printf
(Using printf
)
Since 4th Edition Unix, the portable printf
command has essentially been the new and better echo
. It allows you to use format specifiers to humanize input. To interpret backslash escape sequences, use %b
. The character sequence \n
ensures the output ends with a newline:
由于第4 版的Unix,便携printf
命令基本上已经得到了新的,更好的echo
。 它允许您使用格式说明符来使输入人性化。 要解释反斜杠转义序列,请使用%b
。 字符序列\n
确保输出以换行符结尾:
printf "%b\n" "Many females in Oble are \noblewomen"
# Many females in Oble are
# oblewomen
Though printf
has further options that make it a far more powerful replacement of echo
, this utility is not foolproof and can be vulnerable to an uncontrolled format string attack. It’s important for programmers to ensure they carefully handle user input.
尽管printf
具有更多选项,可以使其更强大地替代echo
,但该实用程序并非万无一失,并且容易受到不受控制的格式字符串攻击。 对于程序员来说,确保他们认真处理用户输入很重要。
将换行符放入变量中 (Putting newlines in variables)
In an effort to improve portability amongst compilers, the ANSI C Standard was established in 1983. With ANSI-C quoting using $'...'
, escape sequences are replaced in output according to the standard.
为了提高编译器之间的可移植性,于1983年建立了ANSI C标准 。使用$'...'
引用ANSI-C ,在输出中根据该标准替换了转义序列 。
This allows us to store strings with newlines in variables that are printed with the newlines interpreted. You can do this by setting the variable, then calling it with printf
using $
:
这使我们可以将带有换行符的字符串存储在已解释换行符的变量中。 您可以通过设置变量,然后使用$
使用printf
来调用它:
puns=$'\number\narrow\nether\nice'
printf "%b\n" "These words started with n but don't make $puns"
# These words started with n but don't make
# umber
# arrow
# ether
# ice
The expanded variable is single-quoted, which is passed literally to printf
. As always, it is important to properly handle the input.
扩展变量是单引号,将其逐字传递给printf
。 与往常一样,正确处理输入很重要。
额外奖励:壳参数扩展 (Bonus round: shell parameter expansion)
In my article explaining Bash and braces, I covered the magic of shell parameter expansion. We can use one expansion, ${parameter@operator}
, to interpret escape sequences, too. We use printf
’s %s
specifier to print as a string, and the E
operator will properly expand the escape sequences in our variable:
在解释Bash和花括号的文章中,我介绍了shell参数扩展的神奇之处。 我们也可以使用一个扩展名${parameter@operator}
来解释转义序列。 我们使用printf
的%s
说明符将其打印为字符串,并且E
运算符将正确扩展变量中的转义序列:
printf "%s\n" ${puns@E}
# umber
# arrow
# ether
# ice
在人类中说话的持续挑战 (The ongoing challenge of talking in human)
String interpolation continues to be a chewy problem for programmers. Besides getting languages and shells to agree on what certain placeholders mean, properly using the correct escape sequences requires an eye for detail.
对于程序员来说, 字符串插值仍然是一个耐嚼的问题。 除了使语言和shell同意某些占位符的含义外,正确使用正确的转义序列还需要关注细节。
Poor string interpolation can lead to silly-looking output, as well as introduce security vulnerabilities, such as from injection attacks. Until the next evolution of the terminal has us talking in emojis, we’d best pay attention when printing output for humans.
不良的字符串插值会导致输出看起来很愚蠢,并引入安全漏洞,例如来自注入攻击 。 直到终端的下一个发展让我们谈论表情符号,在为人类打印输出时,我们最好注意。
翻译自: https://www.freecodecamp.org/news/how-print-newlines-command-line-output/
php 命令行打印换行符