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命令(例如echoprintf ,整个计算时代的技术都试图解决这个问题。

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 \neurologyecho -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 命令行打印换行符

php 命令行打印换行符_如何在命令行输出中打印换行符相关推荐

  1. python如何打印txt文件_关于文本:如何将所有打印导出到Python中的.txt文件?

    本问题已经有最佳答案,请猛点这里访问. Possible Duplicate: Redirect stdout to a file in Python? 我终于完成了我项目的所有代码.现在,我需要所有 ...

  2. cmd执行命令不等待返回值_[CVE20199535] Iterm2命令执行的不完整复现

    CVE-2019-9535 昨天爆出了一个Iterm2的代码执行漏洞,看着非常的刺激吓人,因为我也在用,所以趁热赶紧尝试复现一下.源头文章是来自:https://blog.mozilla.org/se ...

  3. dos命令为java程序赋值_在DOS命令行状态下,如果源程序HelloWorld.java在当前目录下,那么编译该程序的命令是() (5.0分)_学小易找答案...

    [判断题]run方法是运行线程的主体,若run方法运行结束,线程就消亡了 [单选题]在Java 程序中,下面哪个是不合法的标识符() (5.0分) [其它]请提交实验二压缩包 [单选题]在DOS命令行 ...

  4. python换行的转义符_(三)python的转义字符,换行符和除法

    Python的转义字符: \ 在python中,我们知道,数据类型-字符串的写法有多种方式,我们可以是单引号.双引号.三引号都可以表示一个字符串,比如,下面的写法都是等价的 print('hello, ...

  5. mysql怎么用命令行导出sql文件_使用mysql命令行导出sql_MySQL

    bitsCN.com 使用mysql命令行导出sql 其实很简单,只需2个步骤,首先进入mysql命令行,输入:use 数据库名,其次输入:source test.sql 就可以把Test表的数据全部 ...

  6. mysql命令行不支持中文_解决MySQL命令行不支持中文的问题

    MySQL的默认编码是Latin1,不支持中文,那么如何修改MySQL的默认编码呢,下面以UTF-8为例来说明 需要注意的是,要修改的地方非常多,相应的修改方法也很多.下面是一种最简单最彻底的方法: ...

  7. python中怎么打印出表格_怎么使用python脚本实现表格打印?

    大家在办公学习中,有没有被打印机的功能所惊叹?可能大部分小伙伴并没有在意打印机的实现原理,只知道它是可以复印东西的,当小编提出这个问题的时候,那大家有没有考虑过呢?有些小伙伴可能会说这个和我们Pyth ...

  8. mysql句柄是文件描述符_误删除innodb ibdata数据文件 文件句柄 文件描述符 proc fd...

    误删除innodb ibdata数据文件  文件句柄  文件描述符  proc  fd http://www.cnblogs.com/gomysql/p/3702216.html 提示:如果不小心通过 ...

  9. python怎么打印列表长度_关于python:生成器输出的长度

    本问题已经有最佳答案,请猛点这里访问. Python提供了一种很好的方法来获取渴望的可迭代len(x)的长度. 但是对于以生成器理解和函数表示的惰性可迭代对象,我找不到类似的东西. 当然,写这样的东西 ...

最新文章

  1. 浅析移动端建站周期影响因素有哪些?
  2. python 随机生成汉字的三种方法
  3. java提取图片中的文字,深入分析
  4. 使用Pass提高效率
  5. How to check number of Active connections in SQL server?
  6. Lasergene DNASTAR 8.1.3 特别版 Mac 专业的医学生物综合性序列分析工具
  7. 虚拟机VMware16安装教程
  8. 最易难学习的编程语言榜单出炉,C++最难学?
  9. 购买计算机英语情景对话,英语情景对话之购物英语:买相机
  10. cad安装计算机丢失无法启动不了,Win10无法打开CAD2006提示“计算机中丢失ac1st16.dll”怎么办...
  11. Unity3D IAP Google支付
  12. c++三种排序学习图文笔记(冒泡,插入,快速)
  13. 视频(图像)质量检测
  14. 网络基础:ISO网络七层模型
  15. vue按钮字体大小设置_用Vue模仿antd的样式造UI组件之button
  16. python笔记06: 函数
  17. 常用数据库的图标基本认识
  18. PHP处理 EXCEL 日期格式
  19. CentOS7 下 MySQL 之 PXC 集群部署【Docker+多机多节点】
  20. 梅州组织培养实验室之室内布局建设

热门文章

  1. 异常的捕获 try...catch java
  2. 小动画制作 图片盒子配合定时器 winform 114869633
  3. javascript-封闭函数的定义与使用
  4. 智能合约从入门到精通:Solidity Assembly
  5. Centos7用yum安装完mysql后没有mysqld的问题(mysql中三个包都装过了)
  6. 神州数码代表亚太及日本区荣膺Riverbed 2017年度最佳分销商
  7. float浮点数的四舍五入
  8. 文青不适合看的电影《雪国列车》
  9. android中如何通过代码检测是否有root权限?
  10. Struts2知识点总结大全