SHELL中打印字符时用echo, 当打印N多行字符时,显然ECHO有点吃力。这时候就需要EOF上场了。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
cat <<"EOF"
 /$$   /$$           /$$ /$$                 /$$$$$$$$  /$$$$$$  /$$$$$$$$
| $$  | $$          | $$| $$                | $$_____/ /$$__  $$| $$_____/
| $$  | $$  /$$$$$$ | $$| $$  /$$$$$$       | $$      | $$  \ $$| $$      
| $$$$$$$$ /$$__  $$| $$| $$ /$$__  $$      | $$$$$   | $$  | $$| $$$$$   
| $$__  $$| $$$$$$$$| $$| $$| $$  \ $$      | $$__/   | $$  | $$| $$__/   
| $$  | $$| $$_____/| $$| $$| $$  | $$      | $$      | $$  | $$| $$      
| $$  | $$|  $$$$$$$| $$| $$|  $$$$$$/      | $$$$$$$$|  $$$$$$/| $$      
|__/  |__/ \_______/|__/|__/ \______/       |________/ \______/ |__/      
EOF

Here Document

  专业术语Here Document,是一种特殊的IO重定向,EOF=End of File.

  1. EOF作为分隔符代号,可以写成任意字符,只需要保证前后一样即可.

    1
    2
    3
    4
    
    cat <<abcdefo
    Any char is
    OK!
    abcdefo
  2. 当不需要转义,即想输出变量值是。去掉双引号。 下面输出echo HELLO WORLD

    1
    2
    3
    4
    
    VAR="HELLO WORLD"
    cat <<var
    echo ${VAR}
    var
  3. 高级用法可以输出template文件。这们可以输出文件到template.txt

    1
    2
    3
    4
    5
    6
    
    NAME=wxdlong
    AGE=18
    cat <<template>template.txt
    Hello ${NAME}
    Your age is ${AGE}
    template
  4. 定义带换行的变量值。 echo "${variable}"试试看输出?

    1
    2
    3
    4
    5
    
    variable=$(cat <<SETVAR
    This variable
    runs over multiple lines.
    SETVAR
    )
  5. 当注释用.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    : <<COMMENTBLOCK
    echo "This line will not echo."
    This is a comment line missing the "#" prefix.
    This is another comment line missing the "#" prefix.
    
    &*@!!++=
    The above line will cause no error message,
    because the Bash interpreter will ignore it.
    COMMENTBLOCK
  6. 替换交互输入。