如何使用 heredoc 作为一个文本编辑器
编辑器

> 
> 这个不起眼的终端功能在紧要关头提供一个文本编辑器。
> 
> 
> 


![](/data/attachment/album/202101/03/094710uucu3150at905t15.jpg)


在 Linux 和 Unix 的 shell 中有一个不为人知的功能，它能让你用 [cat](https://opensource.com/article/19/2/getting-started-cat-command) 命令打开一个 do-while 循环。它被称为 heredoc，无论你使用什么 shell，它都能让你或多或少地拥有一个文本编辑器。它的语法是：



```
$ cat << EOF >> example.txt

```

中间的字符串（`EOF`），本质上是一个停止循环的条件。也就是说，如果你在一行中单独输入它，循环就会结束。在循环过程中，无论你在终端中输入什么，都会被管道传送到目标文件中（在本例中）。


### 安装


只要你有一个终端，你就能够启动 heredoc。我在 [Bash](https://opensource.com/article/20/4/bash-sysadmins-ebook)、[tsh](https://opensource.com/article/20/8/tcsh) 和 Korn shell 中使用过这个语法技巧。


### 使用 heredoc


要打开一个 heredoc “会话”，你可以使用带重定向的 `cat` 命令。首先用终止字符串（常见约定是 `EOF`，代表 “End Of File”，但它实际上可以是任何字符串）指向 `cat` 命令。在终止字符串之后，将输出重定向到一个目标文件。然后，你就可以直接在终端中输入了，可以使用最常见的 shell 键盘快捷键来处理你的工作。当你在一行上输入你指定的终止字符串时，你的会话就结束了。你可以通过唯一的提示符（通常是 `>`）知道你是在一个 heredoc 循环中。



```
$ cat << EOF >> example.txt
> Everything you type here will be placed into example.txt when I type EOF on a line by itself. Until then, you can type...
>
> whatever...
>
> you want to type.
>
> EOF
$  

```

在终端等待 `EOF` 时，你输入的所有内容都会被放入目标文件中，提示符被忽略，`EOF` 本身也不是文件的一部分。



```
Everything you type here will be placed into example.txt when I type EOF on a line by itself. Until then, you can type...

whatever...

you want to type.

```

在现实中，你可能不会用 heredoc 语法来代替一个正常的文本编辑器。它是一个很好的快速处理方式，可以输入多行，但超过 10 行左右就开始限制它的作用了。例如，如果不触发你 shell 的 [history](https://opensource.com/article/20/6/bash-history-commands) 功能，你就不能编辑以前的行。根据你的 shell 和配置，你可能需要先按向上键，然后按向下键来找回你的文本，然后用 `Ctrl+B` 来后退。


你的 shell 的大部分功能都能正常工作，但可能没有撤销功能，也没有什么错误恢复功能。


此外，即使是最简安装的系统，可能也至少安装了 [Vi](https://opensource.com/article/19/3/getting-started-vim) 或 [ed](https://opensource.com/article/20/12/gnu-ed)。


然而 heredoc 还是很有用的！它比 `echo` 更灵活，当你在编写 shell 脚本时，它是不可缺少的。例如，想象一下你正在编写一个安装脚本，以便你可以自动安装一组自定义应用。其中一个应用没有生成 `.dekstop` 文件，所以它不会出现在你的应用菜单中。为了解决这个问题，你决定在安装时生成一个 `.desktop` 文件。


与其编写一个 `.desktop` 文件，然后作为安装脚本的外部依赖，不如在安装脚本中使用 heredoc：



```
#!/bin/sh

VERSION=${VERSION:-x.y.z}
PKGNAM=${VERSION:-example}
PKG="${PKGNAM}"-"${VERSION}"-`arch`.tgz

# download package
wget "${PKG}"
tar txvf "${PKG}"

# use here doc to create missing .desktop file
cat << EOF >> $HOME/.local/share/applications/example.desktop
[Desktop Entry]
Version=1.0
Type=Application
Name="${PKGNAM}"
Comment="${PKGNAM}"
Exec="${PKGNAM}" %F
EOF

# insert the rest of an install script...

```

你自动地将文本输入到了一个文件中，而不需要文本编辑器（当然，除了你用来写脚本的那个）。下面是生成的 `.desktop` 文件的样子：



```
[Desktop Entry]
Version=1.0
Type=Application
Name=example
Comment=example
Exec=example %F

```

正如你所看到的，你可以在 heredoc 中使用变量，而且它们得到了正确的解析。`EOF` 字符串并没有出现在文件中，它只是标志着 heredoc 的结束。


### 比 echo 更好


heredoc 技术通常被认为比 `echo` 或 [printf](https://opensource.com/article/20/8/printf) 更容易，因为一旦你“进入”了文档，你就可以自由地做任何你想做的事情。从这个意义上说，它是自由的，但与合适的文本编辑器相比，它是有限的。


使用 heredoc 来做快速笔记和 shell 脚本，再也不用为如何动态生成配置文件而烦恼了。




---


via: <https://opensource.com/article/20/12/heredoc>


作者：[Seth Kenlon](https://opensource.com/users/seth) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
There’s a somewhat obscure feature in Linux and Unix shells that allows you to open a sort of do-while loop for the [cat](https://opensource.com/article/19/2/getting-started-cat-command) command. It’s called the *heredoc*, and it enables you to have, more or less, a text editor no matter what shell you’re using. The syntax is:

`$ cat << EOF >> example.txt`


The string in the middle is, essentially, a conditional that stops the loop. That is, if you type alone on a line, the loop ends. During the loop, whatever you type into your terminal is piped into the destination file (in this case).

## Installing


As long as you have a terminal, you already have the ability to initiate a heredoc. I’ve used this syntactical trick in [Bash](https://opensource.com/article/20/4/bash-sysadmins-ebook), [tsch](https://opensource.com/article/20/8/tcsh), and Korn shell.

## Using heredoc

To open a heredoc "session", you use the cat command with redirection that points first to cat with a terminating string (common convention is **EOF** for "End Of File", but it can actually be anything). After the terminating keyword, you redirect your output to a destination file. You're then able to type directly into your terminal, using most common shell keyboard shortcuts to navigate through your work. Your session ends when you type your designated terminating string on a line by itself. You know you're in a heredoc loop by the unique prompt (usually the **> **character).

```
$ cat << EOF >> example.txt
> Everything you type here will be placed into example.txt when I type EOF on a line by itself. Until then, you can type...
>
> whatever...
>
> you want to type.
>
> EOF
$
```


Everything you enter while your terminal is waiting for **EOF** is placed into the destination file. Prompt characters are omitted, and EOF itself is not part of the file.

```
Everything you type here will be placed into example.txt when I type EOF on a line by itself. Until then, you can type...
whatever...
you want to type.
```


Realistically, you’re probably not going to use heredoc syntax as a substitute for a good text editor. It's a great quick hack to enter more than one line, but more than 10 lines or so starts to strain its usefulness. For instance, you can’t go up to edit previous lines without triggering your shell’s [history](https://opensource.com/article/20/6/bash-history-commands) function. Depending on your shell and how it's configured, you may be able to go up, then down to recall your text, and then move back through your text with **Ctrl+B**.

Most features of your shell work as expected, but there’s probably no undo and very little error recovery.

And besides, even the most minimal of installs are likely to have at least [Vi](https://opensource.com/article/19/3/getting-started-vim) or [ed](https://opensource.com/article/20/12/gnu-ed) installed.

And yet heredoc is still useful! It's more flexible than **echo**, and when you’re working on a shell script, it's indispensable. For instance, imagine you’re writing an installer script so you can automate the install of a set of custom applications. One of the applications isn’t distributed with a `.dekstop`

file, so it does not appear in your Application menu. To fix this, you decide to generate a `.desktop`

file at install time.

Rather than writing a `.desktop`

file and carrying it around as an external dependency for your install script, you could use heredoc in your install script itself:

```
#!/bin/sh
VERSION=${VERSION:-x.y.z}
PKGNAM=${VERSION:-example}
PKG="${PKGNAM}"-"${VERSION}"-`arch`.tgz
# download package
wget "${PKG}"
tar txvf "${PKG}"
# use here doc to create missing .desktop file
cat << EOF >> $HOME/.local/share/applications/example.desktop
[Desktop Entry]
Version=1.0
Type=Application
Name="${PKGNAM}"
Comment="${PKGNAM}"
Exec="${PKGNAM}" %F
EOF
# insert the rest of an install script...
```


You have automated text entry into a file, no text editor involved (except the one you use to write your script, obviously). Here’s what the resulting `.desktop`

file looks like:

```
[Desktop Entry]
Version=1.0
Type=Application
Name=example
Comment=example
Exec=example %F
```


As you can see, you can use variables within the heredoc, and they’re correctly resolved. The `EOF`

string doesn’t appear in the file; it only signals the end of the heredoc.

## Better than echo

The heredoc technique is generally considered easier than `echo`

or [printf](https://opensource.com/article/20/8/printf) because once you’re "in" the document, you’re free to do whatever you want. It’s liberating in that sense, but it’s limited compared to a proper text editor.

Use heredoc for quick notes and for shell scripts, and never puzzle over how to dynamically generate configuration files again.

## Comments are closed.