So, what’s “Here Docs”? Well, if you don’t know what it is yet, then hopefully by the time you finish reading this post you’ll know what it is.

Here docs or here documents is a special block code, of which we can use a form of I/O redirection to feed a command list to an interactive program, e.g. cat or the ex text editor according to [1].

Here is an example where a file basic-ingress.yaml was created using here docs.

➜ cat << EOF | tee -a basic-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: basic-ingress
spec:
  rules:
  - host: web.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: web
            port:
              number: 80
---
EOF

In the example above, the special symbol << is followed by the limit string, EOF, which separates the command list (the actual stuff we want to feed or redirect to the stdin of the command or program, e.g. cat in this case). The | (pipe operator [2]) takes the stdout of the command to the left and pipes it as stdinto the command on the right.

As a result the content of the basic-ingress.yaml contains:

➜ cat basic-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: basic-ingress
spec:
  rules:
  - host: web.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: web
            port:
              number: 80
---

The general syntax for here docs is:

$ COMMAND <<InputComesFromHERE
...
...
...
InputComesFromHERE

Do note that, we are not restricted to only EOF for limit string, though EOF is widely adopted as it happens to stand for ’end-of-file’. But, we can use anything, for example:

➜ cat << IS-IT-REALLY-EOF
-------------------------------------
This is line 1 of the message.
This is line 2 of the message.
This is line 3 of the message.
This is line 4 of the message.
This is the last line of the message.
IS-IT-REALLY-EOF

If you want to learn more about it, head to the reference [1] at the link below.

References: