简体   繁体   English

Bash语法:什么是“<<”?

[英]Bash syntax: What is the “<<”?

Could someone explain the "<<" in the following code? 有人可以在下面的代码中解释“<<”吗?

mysql test<<E0Q
Select * from signins

I'd try to search for it myself, but symbols are hard to search for... 我会尝试自己搜索,但很难找到符号......

Thanks, Dan 谢谢,丹

这是一个“here file”,请参阅http://www.linuxjournal.com/article/3771

这里是docs ,或者是一种将大型文本块轻松管理到程序中的方法。

It's not only used for piping though. 它不仅用于管道。 For example, the scripts used in the Linux From Scratch walkthroughs make extensive use of heredocs in combination with the cat command and the output redirection operator ( > ). 例如,Linux From Scratch演练中使用的脚本与cat命令和输出重定向运算符( > )结合使用了heredocs。 Here is an example of such a thing: 这是一个这样的例子:

user@domain ~$ cat >test.c <<EOF
int main(void){return 0;}
EOF
user@domain ~$

That writes all of the text between the <<EOF start delimiter and the EOF end delimiter to a file `test.c' and once the EOF end delimiter is encountered, you're returned to the shell prompt. 这会将<< EOF开始分隔符和EOF结束分隔符之间的所有文本写入文件`test.c',一旦遇到EOF结束分隔符,就会返回到shell提示符。

They are called here-documents. 它们在这里被称为文件。 From the manual: 从手册:

Here Documents 这里的文件

 This type of redirection instructs the shell to read input from the current source until a line containing only word (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command. The format of here-documents is: <<[-]word here-document delimiter 

If you pipe echo into read , for example, the variable assignment is "lost" since read gets executed in a subshell. 例如,如果将echoread ,则变量赋值将“丢失”,因为read在子shell中执行。 You can use a here-doc or a here-string to make it execute in the current shell: 您可以使用here-doc或here-string使其在当前shell中执行:

read -a var <<< "some text"

That's an example of another construct you may see called a "here-string", which is similar to a here-doc. 这是您可能看到的另一个构造的示例,称为“here-string”,类似于here-doc。

See this and this . 看到这个这个

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

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