php命令⾏打印换⾏符_如何在命令⾏输出中打印换⾏符
php 命令⾏打印换⾏符
Surprisingly, getting computers to give humans readable output is no easy feat. With the introduction of 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 to modern shell commands like echo and printf.
令⼈惊讶的是,让计算机为⼈类提供可读的输出并⾮易事。 通过引⼊ ,特别是标准输出,程序获得了⼀种使⽤纯⽂本流进⾏对话的⽅法。但是⼈性化和显⽰标准输出是另⼀回事。 从使⽤到现代的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 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 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, require a -e option to do the same:
从在到现代的类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 using command substitutions.
由于实现中的这些不⼀致, echo被认为是不可移植的。 另外,它与⽤户输⼊的结合使⽤相对容易通过使⽤命令替换的来破坏。
In modern systems, it is retained only to provide compatibility with the many programs that still use it. The the use of printf in new programs.
在现代系统中,保留它只是为了提供与仍在使⽤它的许多程序的兼容性。 在新程序中使⽤printf 。
使⽤printf (Using printf)
Since 4th Unix, the portable has essentially been the new and better echo. It allows you to use to humanize input. To
php的工作流程
interpret backslash escape sequences, use %b. The character sequence \n ensures the output ends with a newline:
由于第4 的Unix,便携基本上已经得到了新的,更好的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 attack. It’s important for programmers to ensure they .
尽管printf具有更多选项,可以使其更强⼤地替代echo ,但该实⽤程序并⾮万⽆⼀失,并且容易受到攻击。 对于程序员来说,确保他们很重要。
将换⾏符放⼊变量中 (Putting newlines in variables)
In an effort to improve portability amongst compilers, the was established in 1983. With using $'...', are replaced in output according to the standard.
为了提⾼编译器之间的可移植性,于1983年建⽴了 。使⽤$'...' ,在输出中根据该标准替换了 。
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 , I covered the magic of . 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:
在解释⽂章中,我介绍了的神奇之处。 我们也可以使⽤⼀个扩展名${parameter@operator}来解释转义序列。 我们使⽤printf的%s说明符将其打印为字符串,并且E运算符将正确扩展变量中的转义序列:
printf "%s\n" ${puns@E}
# umber
# arrow
# ether
# ice
在⼈类中说话的持续挑战 (The ongoing challenge of talking in human)
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 . Until the next evolution of the terminal has us talking in emojis, we’d best pay attention when printing output for humans.
不良的字符串插值会导致输出看起来很愚蠢,并引⼊安全漏洞,例如来⾃ 。 直到终端的下⼀个发展让我们谈论表情符号,在为⼈类打印输出时,我们最好注意。
php 命令⾏打印换⾏符

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。