The cat command, used frequently in bash scripting, excels in concatenating and displaying file content. Although handy, writing multi-line strings or scripts directly from the command line can often become cumbersome. This is where “Here Documents”, or “heredocs”, come to the rescue, allowing an interactive way to input a multi-line string into a file. In this blog post, we’ll explore the various uses of ‘heredocs’ in different scenarios.

Creating Files with Multi-Line Content

Using cat, EOF, <<, and >, we can effortlessly place a multi-line string into a file. Here, EOF is the end-of-file character used to signify the end of the input. The > operator redirects the input to the specified file. The << operator, on the other hand, sets the end-of-content marker, which is EOF in this case.

For instance, consider creating a file, mydata.txt, with multiple lines of content:

cat << EOF > mydata.txt
John
James
Alex
EOF

This script creates mydata.txt with three lines of text.

Similiarly, we can generate a script file:

cat << EOF > backup.sh
#!/bin/bash
tar cvf /home/john
EOF

In this case, the script creates backup.sh, a bash script for backing up the ‘/home/john’ directory.

Assigning Multi-line Strings to Variables

The combination of cat, EOF, and << also assists in assigning a multi-line string to a bash variable. The cat command prints the string, while the $() operator assigns it to a variable:

comm =$(cat <<EOF
ls /home/john/Downloads/
rm /home/john/Downloads/*
EOF
)

Piping Multi-line Strings

You can also utilize cat, EOF, and the pipe (|) operator to redirect a multi-line string to a pipe and command. For example, the following script greps the content for the string “m”. The lines with matches are redirected to the tee command, which then writes the content to a file named matched_names.txt:

cat <<EOF | grep 'm' | tee matched_names.txt
John
James
Alex
Elise
Matthew
EOF

Understanding Here Document and Here String

Two critical concepts in bash scripting are “Here Document” (<<) and “Here String” (<<<).

A Here Document is a section of a shell script that directs the command to read input from the current source until a line containing only the delimiter is seen. All of the lines read up to that point are then used as the standard input for a command. Consider the following example:

$ wc << EOF
> one two three
> four five
> EOF
 2  5 24

Here String, on the other hand, is a variant of Here Documents. Instead of grabbing input from multiple lines, a Here String works with pre-made strings of text. It behaves somewhat similar to echo '5*4' | bc, but without needing to run the command interactively.

bc <<< 5*4

Bash implements Here-strings via temporary files, which occupy some memory space temporarily but do not show up in the list of /tmp directory entries. The shell and the command later reference these files.

Process Substitution in Bash

Process substitution in bash is quite similar to piping the stdout of one command to another. Notably, it allows redirecting the output of multiple commands. It’s denoted as <(list) in the bash manpage, hinting at the ability to redirect the output of multiple commands or a list of commands.

These utilities and features enhance bash’s capability, making it a robust tool for handling a wide range of scripting tasks. Mastering them opens up a whole new world of scripting possibilities. Whether you’re a system administrator or a developer working on Linux, understanding these concepts will undeniably help streamline your tasks.



Buy Me a Coffee