What is EOF!! in the bash script?

2019-03-25 05:17发布

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!!.

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! ?

Plus, why and when should we use EOF!! instead of EOF?

5条回答
成全新的幸福
2楼-- · 2019-03-25 06:00

The bash manual lists this under "Event designators", saying:

!!

Refer to the previous command. This is a synonym for !-1`.

I simply searched for "bash manual double exclamation".

查看更多
Root(大扎)
3楼-- · 2019-03-25 06:03

It's probably just a weird heredoc.

Example:

cat << EOF!!
blabla
EOF!!

Note: this only works in script files. The command line parser interprets !!.

查看更多
萌系小妹纸
4楼-- · 2019-03-25 06:06

You can use whatever string as here document terminator.

EOF!! is just what the person writing the script decided to use.

查看更多
Rolldiameter
5楼-- · 2019-03-25 06:17

As others already wrote, this is a 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.

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.

查看更多
乱世女痞
6楼-- · 2019-03-25 06:18

On the command line, !! would be expanded to the last command executed. Bash will print the line for you:

$ 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
查看更多
登录 后发表回答