cat << EOF
参考链接:https://stackoverflow.com/questions/2500436/how-does-cat-eof-work-in-bash
The
cat <<EOFsyntax is very useful when working with multi-line text in Bash, eg. when assigning multi-line string to a shell variable, file or a pipe.
在 bash 中想操作多行时, cat << EOF 这个语法非常实用。
1. 将多行字符串赋值给 shell 变量。
Assign multi-line string to a shell variable
$ sql=$(cat <<EOFSELECT foo, bar FROM dbWHERE foo='baz'EOF)
The $sql variable now holds the new-line characters too. You can verify with echo -e "$sql".
注意, _echo $sql_ 输出时是一行的,上面的命令会分行。
2. 将多行字符串写入文件
Pass multi-line string to a file in Bash。
- 将多行字符串写入文件,如果该文件不存在则创建.
- 如果该文件已存在,
cat <<EOF > print.sh是 truncate 操作; 如果该文件已存在,
cat <<EOF >> print.sh是 append 操作;$ cat <<EOF > print.sh#!/bin/bashecho \$PWDecho $PWDEOF
The
print.shfile now contains:#!/bin/bashecho $PWDecho /home/user
命令解析
cat <<EOF是把下面要传递的内容传给 cat 命令;> print.shcat 把接收的数据传递给print.sh。>会对 print.sh 先执行 truncate 操作。>>是进行 append 操作。EOF, 表示向 cat 写入数据的完结。实战
mysql 使用东八区。root@16da4b52cf9b:/# cat <<EOF > /etc/mysql/conf.d/mysql.cnf> [mysqld]> default-time-zone='+08:00'> EOF
3. Pass multi-line string to a pipe in Bash
$ cat <<EOF | grep 'b' | tee b.txtfoobarbazEOF
The b.txt file contains bar and baz lines. The same output is printed to stdout.
