Linux 什么是EOF!!在 bash 脚本中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18527121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is EOF!! in the bash script?
提问by Marcus Thornton
There is a command I don't understand:
有一个我不明白的命令:
custom_command << EOF!!
I want to ask what EOF!! is in the bash script. I did find EOF with google, but google will ignore the "!!" automatically, so I cannot find EOF!!.
我想问什么EOF!!在 bash 脚本中。我确实用谷歌找到了 EOF,但谷歌会忽略“!!” 自动,所以我找不到 EOF !!。
I know the end of the file token, but I don't exactly know what it means with the "!!" in the script. Is this a mark to force something to do something like in vim's wq! ?
我知道文件标记的结尾,但我不完全知道“!!”是什么意思 在脚本中。这是强制某些事情做一些事情的标志,比如在 vim 的 wq 中!?
Plus, why and when should we use EOF!! instead of EOF?
另外,我们为什么以及何时应该使用 EOF !!而不是EOF?
采纳答案by choroba
On the command line, !!
would be expanded to the last command executed. Bash will print the line for you:
在命令行上,!!
会扩展到最后执行的命令。Bash 将为您打印该行:
$ ls
a.txt b.txt
$ cat <<EOF!!
cat <<EOFls
>
In a script, though, history expansion is disabled by default, so the exclamation marks are part of the word.
但是,在脚本中,默认情况下禁用历史扩展,因此感叹号是单词的一部分。
#! /bin/bash
ls
cat <<EOF!!
echo 1
EOFls
echo 2
Produces:
产生:
a.txt b.txt
script.sh: line 7: warning: here-document at line 3 delimited by end-of-file (wanted `EOF!!')
echo 1
EOFls
echo 2
To enable history and history expansion in a script, add the following lines:
要在脚本中启用历史和历史扩展,请添加以下几行:
set -o history
set -H
回答by unwind
The bash manuallists this under "Event designators", saying:
bash 手册在"Event designators" 下列出了这一点,说:
!!
Refer to the previous command. This is a synonym for !-1`.
!!
参考上一条命令。这是 !-1` 的同义词。
I simply searched for "bash manual double exclamation".
我只是搜索“bash 手动双感叹号”。
回答by jlliagre
You can use whatever string as here document terminator.
您可以使用任何字符串作为此处的文档终止符。
EOF!!
is just what the person writing the script decided to use.
EOF!!
正是编写脚本的人决定使用的。
回答by glglgl
As others already wrote, this is a here-document.
正如其他人已经写的那样,这是一个 here-document。
The token used for that should be chosen carefully; as the probability that the here-document contains EOF!!
is lower than for EOF
itself, they chose that.
应谨慎选择用于此的令牌;由于 here-document 包含的概率EOF!!
低于EOF
它本身,他们选择了那个。
I suppose they checked it does not harm before using it; !!
in a script does NOT refer to the history, but it stays as it is.
我想他们在使用前检查过它没有害处;!!
在脚本中不引用历史,但它保持原样。