Posted on
Advanced

Here documents and here strings in scripts

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Mastering Here Documents and Here Strings in Bash Scripts

In the world of Linux shell scripting, manipulating text is a common task. Bash, one of the most popular shell environments, provides powerful tools for text handling, among which 'here documents' and 'here strings' are especially useful for managing multi-line strings and feeding them into commands. In this article, we’ll dive deep into understanding these features and how to effectively use them in your bash scripts.

What are Here Documents?

A 'here document' (also known as a heredoc) is a type of redirection that allows you to pass multiple lines of input to a command. Here documents are generally used when a large block of input needs to be fed to a command.

Here’s the basic syntax of a here document:

command <<[delimiter]
[Your multi-line text]
[delimiter]

In this syntax, command is any command that reads from standard input, << is the redirection operator followed by a 'delimiter' that marks the start and end of the text input.

Here’s an example using cat:

cat <<EOF
Hello,
this is a text
spread over
multiple lines.
EOF

This script would output:

Hello,
this is a text
spread over
multiple lines.

What are Here Strings?

A 'here string' is a simpler version of a here document, intended for sending a single line of text to a command. It uses a slightly different syntax:

command <<< "Your string"

Here, command is any command that accepts standard input, <<< introduces the here string, and "Your string" is the text that you want to send.

For example:

cat <<< "Hello, this is a single line."

This would produce:

Hello, this is a single line.

Practical Examples in Scripts

Here documents and here strings become particularly useful in scripting when used for configuring applications, scripting database queries, or creating email bodies right within the shell script. Below we'll go through a practical example for each.

Example 1: Configuring an Application

Suppose you're writing a script to configure a custom application that accepts configurations via standard input:

#!/bin/bash
application_configure <<EOF
username: example_user
password: example_pass
EOF

Example 2: Automating Database Queries

If you're dealing with databases, you can use here documents to pass SQL commands:

#!/bin/bash
mysql -u root -p"your_password" <<MYSQL
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE users (name VARCHAR(100));
INSERT INTO users (name) VALUES ('Alice'), ('Bob');
SELECT * FROM users;
MYSQL

Example 3: Sending Emails

When scripting an email sending functionality, here documents can be used to create the body:

#!/bin/bash
/usr/sbin/sendmail -oi -t <<EOF
From: sender@example.com
To: recipient@example.com
Subject: Here Document Example

Hi there,
This is an email body.
EOF

Installation of Bash and Updates

To make sure you can use these features, you need to have Bash installed and updated on your system. Here’s how you can install or update Bash on different distributions using different package managers:

Debian/Ubuntu (apt):

sudo apt update
sudo apt install bash

Fedora (dnf):

sudo dnf install bash

openSUSE (zypper):

sudo zypper install bash

By using here documents and here strings, you can simplify many scripting tasks that involve multi-line strings by incorporating them directly into your bash scripts. Whether for automated emails, database management, or application configuration, mastering these tools can greatly enhance your scripts' flexibility and readability.