here文档

u-boot下的mkconfig中使用了

cat << EOF >> config.h

here文档的作用是:把一系列需要从键盘输入的内容,模拟人工输入,一行一行的交给命令处理。

例如:

cat >> config.h << EOF
#define CONFIG_BOARDDIR board/$BOARDDIR
#include <config_cmd_defaults.h>
#include <config_defaults.h>
#include <configs/${CONFIG_NAME}.h>
#include <asm/config.h>
EOF

注意两者的区别

cat >> config.h << “EOF”
#define CONFIG_BOARDDIR board/$BOARDDIR
#include <config_cmd_defaults.h>
#include <config_defaults.h>
#include <configs/${CONFIG_NAME}.h>
#include <asm/config.h>
EOF
$ tr a-z A-Z <<END_TEXT
> one two three
> uno dos tres
> END_TEXT
ONE TWO THREE
UNO DOS TRES

以下引述自:http://www.jsxubar.info/shell-script-here-document.html

一个here document就是一段带有特殊目的的代码段. 它使用I/O重定向的形式将一个命令序列传递到一个交互程序或者命令。

limit string用来界定命令序列的范围. 特殊符号<<用来标识limit string. 这个符号的作用就是将文件的输出重定向到程序或命令的stdin中.
here document看上去是下面这个样子:

#!/bin/bash
interactive-program <<LimitString
command
…
LimitString

选择一个名字非常诡异limit string能够有效的避免命令列表与limit string重名.
结尾的limit string, 就是here document最后一行的limit string, 必须从第一个字符开始. 它的前面不能够有任何前置的空白. 而在这个limit string后边的空白也会引起异常. 空白将会阻止limit string的识别.
-选项用来标记here document的limit string (<<-LimitString), 可以抑制输出时前边的tab(不是空格). 这么做可以增加一个脚本的可读性.

在here document的开头, 引用或转义”limit string”, 会使得here document消息体中的参数替换被禁用.
#  cat <<”Endofmessage”
禁用了参数替换后, 将允许输出文本本身. 如果你想产生脚本甚至是程序代码的话, 那么可以使用这种办法.

可以将here document的输出保存到变量中.

variable=$(cat <<SETVAR
This variable
runs over multiple lines.
SETVAR)

可以这么使用:(冒号), 做一个假命令来从一个here document中接收输出. 这么做事实上就是创建了一个”匿名”的here document. 可以用来”注释”掉代码块. 能够产生”自文档化(self-documenting)”的脚本.

#! /bin/bash
echo "hello world"
: << EOF
balblabla
dingo
EOF
echo "good bye"

对于那些使用”here document”, 并且非常复杂的任务, 最好考虑使用expect脚本语言, 这种语言就是为了达到向交互程序添加输入的目的而量身定做的.

====================================================================================

here string可以看成是here document的一种定制形式. 除了COMMAND <<<$WORD, 就什么都没有了, $WORD将被扩展并且被送入COMMAND的stdin中.

引述自:http://linux.die.net/abs-guide/x15683.html

A here string can be considered as a stripped-down form of a here document. It consists of nothing more than COMMAND <<<$WORD, where $WORD is expanded and fed to the stdin of COMMAND.

# Instead of:
if echo "$VAR" | grep -q txt   # if [[ $VAR = *txt* ]]
# etc.

##########################################################################
##########################################################################
# Try:
if grep -q "txt" <<< "$VAR" then echo "$VAR contains the substring sequence "txt"" fi # Thank you, Sebastian Kaminski, for the suggestion.
String="This is a string of words."

read -r -a Words <<< "$String"
#  The -a option to "read"
#+ assigns the resulting values to successive members of an array.

echo "First word in String is:    ${Words[0]}"   # This
echo "Second word in String is:   ${Words[1]}"   # is
echo "Third word in String is:    ${Words[2]}"   # a
echo "Fourth word in String is:   ${Words[3]}"   # string
echo "Fifth word in String is:    ${Words[4]}"   # of
echo "Sixth word in String is:    ${Words[5]}"   # words.
echo "Seventh word in String is:  ${Words[6]}"   # (null)
                                                 # Past end of $String.

# Thank you, Francisco Lobo, for the suggestion.
原文地址:https://www.cnblogs.com/openix/p/3519230.html