Even if there are so many modern languages like Golang, Bash still remains the favorite scripting language for many. Automating and managing processes on Unix-based systems are easily done with Bash. This is because Bash has a plethora of powerful utilities like the one, we are about to see now: bash Heredoc.
In Bash, Heredoc allows multi-line input or strings. This makes Heredoc a helpful tool to embed large blocks of text or commands in a script. In real world applications, scripts often interact with other programs or generate config files. In cases like these in Bash, heredoc becomes very helpful.
This article, an addition to our scripting guides in Bash, Heredoc is explained in detail. We will start with what heredoc is, how to use heredoc, and some practical examples to help you understand how you can use it.
What is Bash Heredoc?
Heredoc is short for Here Document. This is a method of redirecting input or strings that extend for multiple lines, into a command or file in Bash. The main advantage is that Heredoc allows you to handle large blocks of text or input without the need to escape special characters or long echo commands.
Here is the basic syntax of heredoc:
command <<-DELIMITER Content comes here DELIMITER
Let us explain each of the params used here:
- Command: The command that accepts input. The command can be anything like cat, echo, or even a program.
- Delimiter: This is the user-defined string that signals the beginning and end of the heredoc content. Commonly used delimiters are EOF, END, and INPUT.
The content between the DELIMITER markers is the text you want to pass to the command. In bash, Heredoc can be used to provide input for many shell commands. It can be used to create files, modify directories, or embed large blocks of text without hassle.
Some Examples of Heredoc in Bash
To give a simple start, here is an example where heredoc is used along with the cat command to generate a multi-line message
cat <<EOF This is an example of heredoc in Bash. Here is where we write multiple lines of text. Long live Unixmen. I love Bash. EOF
When this script is run, the output will be:
This is an example of heredoc in Bash. Here is where we write multiple lines of text. Long live Unixmen. I love Bash.
Some Practical Use Cases for Bash Heredoc
How to Create Configuration Files
With Heredoc, you can create configuration files within a script. Without the pain of writing a file line by line in Bash, Heredoc lets you define the file’s entire content at once. Let us see a sample script to see how we can do this.
cat <<EOF > config.txt # Configuration file user=unixmen password=TopSecret host=localhost EOF
When this script is run, a config.txt file will be generated with the specified content.
How to Pass Input Commands
In cases where you want to pass a block of input to a command, Heredoc can be useful. Let us see how you can use heredoc to pass commands to an interactive program like mysql.
mysql -u root -p <<EOF CREATE DATABASE unixmen; USE unixmen; CREATE TABLE users (id INT, name VARCHAR(50)); EOF
When this script is run, heredoc provides input to mysql directly. This creates a new database and table.
How to Embed Scripts or Code
In Bash, Heredoc lets you embed entire scripts or code snippets inside another script. This makes it easier to distribute systems with multiple parts.
python <<EOF print("Hello from Unixmen!") for i in range(3): print(f"Loop {i}") EOF
This script will execute a Python script within the Bash script itself. Life is easier with Heredoc.
Commonly Used Options in Heredoc
Indentation Handling
A commonly encountered issue in Heredoc is that indentation within the content will be preserved. This makes the script hard to read. You can bypass this by using a hyphen before the delimiter. This lets you use indentation in your script while ignoring it in the output.
Let us look at a sample syntax.
cat <<-EOF This text will be properly formatted. Indentation in this script will be ignored. Pretty useful option. EOF
Running this script will get the output:
This text will be properly formatted. Indentation in this script will be ignored. Pretty useful option.
Variable Expansion
Heredoc allows variable expansion by default. The variables inside the heredoc content will be replaced by their values. Here is an example to get you started:
name="Ubuntu" cat <<EOF Hello, $name! EOF
This script will generate the output:
Hello, Ubuntu!
If you do not want the variables to be expanded, you can put the delimiter within quotes.
Wrapping Up
Since we have covered everything good about bash Heredoc, let us now wind up with some security considerations. One common pitfall while using Heredoc is, when you use commands like ssh or sudo, take extra precautions to avoid passing sensitive data into scripts. This may expose credentials in shell history or logs. To ensure you handle this scenario securely, use encrypted files or environment variables to avoid hardcoding sensitive data.
Related Link
An interesting use case for heredoc