Here document, another way to write multiline into one file

Here documents are indeed a powerful feature in Unix-like operating systems, allowing for the efficient creation and manipulation of files from within a shell script. They are particularly useful when you need to insert multiple lines of text into a file without having to resort to looping constructs or temporary files.

Basic Usage

Here (aka here-document, here-text, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also used for a form of multiline string literals that use similar syntax, preserving line breaks and other whitespace (including indentation) in the text.

Here are some sample:

Basically, you can write as follow

$ cat << delimiting_identifier
... your text here
... your text here another line
... delimiting_identifier
your text here
your text here another line

you can replace “cat” using tr, read or something else, you can also replace delimiting_identifier using other word.

By default, behavior is largely identical to the contents of double quotes: variables are interpolated, commands in backticks are evaluated, etc.

$ cat << delimiting_identifier
... $(whoami)
... delimiting_identifier
yu

To prevent this replacement, we can write as follow:

$ cat << 'delimiting_identifier'
... $(whoami)
... delimiting_identifier
$(whoami)

It is a very important feature for us.

We can easily generate some files in script, and write multiline lines to this file.

Advanced Usage

Redirection

You can redirect the output of a here document to a file directly. This is done by appending > or >> to the command that initiates the here document. For example:

cat < output.txt
This is the first line.
This is the second line.
EOF

This will create a file named output.txt with the specified lines.

Using with Other Commands

Here documents can be used with other commands that read from standard input. For example, you can use tr to translate or delete characters:

tr 'a-z' 'A-Z' < transformed.txt
hello world
EOF

This will convert all lowercase letters to uppercase in the input and write the result to transformed.txt.

Looping Over Here Documents

While here documents are not typically used within loops, you can simulate such behavior by using a loop to generate the delimiter:

for i in {1..5}; do
cat <> numbered.txt
Line $i
EOF
done

This will append lines with numbers to numbered.txt.

Conditional Execution

You can also use here documents in conditional statements. For example, you can write a script that only writes to a file if a certain condition is met:

if [ some_condition ]; then
cat < conditional_output.txt
This will only be written if the condition is true.
EOF
fi

Security Considerations

When using here documents, especially in scripts that are sourced or executed in a multi-user environment, it’s important to consider security implications. Be cautious with the use of variables and command substitutions, as they can lead to injection vulnerabilities if not properly sanitized.

Conclusion

Here documents provide a flexible and efficient way to handle multi-line text in shell scripts. They are a fundamental tool for shell scripting, allowing for complex file manipulations with minimal overhead. Understanding and utilizing here documents can significantly enhance the power and flexibility of your shell scripts.