简体   繁体   English

Unix:我如何读取多行?

[英]Unix: How can I read multiple lines?

So it seems read command only takes 1 line, what if I wanted to input a short paragraph with line breaks? 所以看起来read命令只需要1行,如果我想用换行输入一个短段怎么办? What would I use? 我会用什么? I think there's a command that ends when you press ctrl+d, so it return key is used for starting a new paragraph. 我认为有一个命令在你按ctrl + d时结束,所以它返回键用于启动一个新段落。

text=$(cat)

Allows you to type text with newlines, terminated by a Ctrl-d at the beginning of a line. 允许您键入带换行符的文本,在行的开头用Ctrl-d结束。

The equivalent in Bash, without needing to use an external utility is Bash中的等价物,无需使用外部实用程序

text=$(</dev/stdin)

You could drop the user into their favorite editor, with commented text that explains what information is needed and how to make paragraph breaks. 您可以将用户放入他们喜欢的编辑器中,使用注释文本说明需要哪些信息以及如何制作分段符。 That's how most vcs commits work. 这就是大多数vcs提交工作的方式。 See ldapvi for a more sophisticated example of this kind of interface. 有关此类接口的更复杂示例,请参阅ldapvi。

如果换行中不包含换行符,则可以使用“\\”字符来转义输入中的换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM