How to use “Documents here” in Bash on Linux

Contents

A terminal window on a Linux computer system.

The strangers “documents here” enable you to use input redirection / output inside bash scripts on linux. They are a great way to automate the commands you need to run on a remote computer..

Here Documents

Many commands in Linux have two or three letter names.. This is partly what gives rise to the idea that Linux is hard to learn and full of arcane commands.. But one of the strangest names in Linux is not one of the cryptically short. “Here the documents” they are not documents, and it is also not very clear what the “here”.

They are a relatively obscure build, but they are useful. Of course, this is linux, so there is more than one way to skin a cat. Some of the functionality provided by these documents may be reproduced in other ways. These alternative methods are generally more complicated. In programming and scripting, “more complex” also means “more error prone” and that your code is harder to maintain.

Where the docs really shine here is in automating commands you want to send to a remote computer from an established connection from within a script. Making the connection is easy, but once the connection has been made, What “pumps” your commands from your script to the shell on the remote computer? Here the docs enable you to do that very simply.

Document Basics Here

The idiomatic representation of a document here looks like this:

COMMAND << limit_string
 .
 .
text 
data
variables
.
.
limit_string
  • COMMAND: Can be any Linux command that accepts redirected input. Note the echo command does not accept redirected input. If you need to write on the screen, you can use the cat command, what are you doing.
  • <<: The redirect operator.
  • limit_string: this is a label. It can be anything you want as long as it doesn't appear in the data listing that you are redirecting to the command. Used to mark the end of the text list, data and variables.
  • data list: A list of data to be sent to the command. can contain commands, text and variables. The content of the data list is entered into the command one line at a time until _limit_string is found.

You'll probably see examples from here docs that use “EOF” as the limit string. We do not favor that approach. It works, but “EOF” it means “end of file”. Other than the rare case where a startup document is the last thing in a script file, “EOF” is being used wrongly.

It will make your scripts much more readable if you use a limit string that refers to what you are doing. If you are sending a series of commands to a remote computer via Safe cover (SSH), a limit string called something like “_remote_commands” would make a lot of sense. No need to start with an underscore “_” character. We do this because you mark them as out of the ordinary in your script..

simple examples

You can use here docs on the command line and in scripts. When I type the following in a terminal window, you will see a “>“Line continuation message every time you press” Enter “. When you write the limit string “_end_of_text” and tap “Enter”, the list of websites is passed to cat, and displayed in the terminal window.

cat << _end_of_text 
How-To Geek 
Review Geek 
LifeSavvy 
CloudSavvy IT
MindBounce
_end_of_text

That is not the most valuable exercise, but it shows that nothing is sent to the command until the entire list of data is collected and the limit string is found. the cat The command does not receive any input until you enter the limit string “_end_of_text” and press the key “Enter”.

We can do the same in a script. Type or copy this example into an editor, save the file as “heredoc-1.sh” y cierre el editor.

#!/bin/bash

cat << "_end_of_text"
Your user name is: $(whoami)
Your current working directory is: $PWD
Your Bash version is: $BASH_VERSION
_end_of_text

As you follow this post, every time you create a script, shall make it executable before it runs. In each case, Use the chmod command. Replace the script name in each example with the name of the script used here.

chmod +x heredoc-1.sh

This script contains two environment variables, $PWD and $BASH_VERSION. Environment variable names are replaced with their data values (the current working directory and version of Bash) when the script is executed.

The script also uses command substitution about him whoami command. The command name is replaced by its own output. The output of the entire script is written to the terminal window via the cat command.. We execute the script calling it by its name:

./heredoc-1.sh

If you modify the script and put the limit string in the first line of the document here in quotes ” " “, The list of data is passed to the document command here literally. Variable names are displayed instead of variable values ​​and command substitution will not be performed.

#!/bin/bash

cat <<- "_end_of_text"
Your user name is: $(whoami)
Your current working directory is: $PWD
Your Bash version is: $BASH_VERSION
_end_of_text

Tab character handling

By default, the tab characters in your data list will be preserved and written to the terminal window. Copy and save this example as “heredoc-2.sh”. Make it executable using the chmod command. Edit indented lines to ensure they have one or two tab characters at the beginning of the line instead of a series of spaces.

#!/bin/bash

cat << _end_of_text
Your user name is: $(whoami)
  Your current working directory is: $PWD
    Your Bash version is: $BASH_VERSION
_end_of_text
./heredoc-2.sh

Tabs are written to the terminal window.

Adding a script “-”For the redirect operator, the document here will ignore the leading tab characters. Save this example as “heredoc-3.sh” and make it executable.

#!/bin/bash

cat <<- _end_of_text
Your user name is: $(whoami)
  Your current working directory is: $PWD
    Your Bash version is: $BASH_VERSION
_end_of_text
./heredoc-3.sh

Tabs are ignored. This may seem trivial, but it's a good way to deal with the initial tabs due to the indented sections of the scripts.

Loops and other logical constructs are often indented. If your document here is contained in an indented section of a script, use a hyphen “-”With redirection operator eliminates formatting problems caused by leading tab characters.

#!/bin/bash

if true; then
  cat <<- _limit_string
  Line 1 with a leading tab.
  Line 2 with a leading tab.
  Line 3 with a leading tab.
  _limit_string
fi

Redirect to a file

The output of the command used with the document here can be redirected to a file. Use the “>“(Create the file) O”>>“(Create the file if it doesn't exist, add it to the file if it exists) redirect operators after the limit string in the first line of the document here.

It's script es “heredoc-4.sh”. It will redirect its output to a text file called “session.txt”.

#!/bin/bash

cat << _end_of_text > session.txt
Your user name is: $(whoami)
Your current working directory is: $PWD
Your Bash version is: $BASH_VERSION
_end_of_text
./heredoc-4.sh
cat session.text

Pipe the output to another command

The output of the command used in a document here can be piped as input to another command. use the pipe “|“Operator after the limit string in the first line of the document here. Let's pipe the output of the command here document, cat, within sed. We want replace all occurrences of the letter “a” with the letter “e”.

Name this script “heredoc-5.sh”.

#!/bin/bash

cat << _end_of_text | sed 's/a/e/g'
How
To
Gaak
_end_of_text
./heredoc-5.sh

“Gaak” corrects itself to “Geek”.

Sending parameters to a function

The command used with a document here can be a function in the script.

This script passes some vehicle data to a function. The function reads the data as if it had been written by a user. Next, the values ​​of the variables are printed. Save this script as “heredoc-6.sh”.

#!/bin/bash

# the set_car_details() function
set_car_details ()
{
read make
read model
read new_used
read delivery_collect
read location
read price
}

# The here document that passes the data to set_car_details()
set_car_details << _mars_rover_data
NASA
Perseverance Rover
Used
Collect
Mars (long,lat) 77.451865,18.445161
2.2 billion
_mars_rover_data

# Retrieve the vehicle details
echo "Make: $make"
echo "Model: $model"
echo "New or Used: $new_used"
echo "Delivery or Collection: $delivery_collect"
echo "Location: $location"
echo "Price $: $price"
./heredoc-6.sh

Vehicle details are typed into the terminal window.

Create and send an email

We can use a document here to compose and send an email. Note that we can pass parameters to the command in front of the redirection operator. Were using the linux mail command for send an email through the local mail system to the called user account “dave”. the -s The option (affair) allows us to specify the subject of the email.

This example forms the script “heredoc-7.sh”.

#!/bin/bash

article="Here Documents"

mail -s 'Workload status' dave << _project_report
User name: $(whoami)
Has completed assignment:
Article: $article
_project_report
./heredoc-7.sh

There is no visible output from this script. But when we check our mail, we see that the email was redacted, shipped and delivered.

mail

Using documents here with SSH

Here docs are a powerful and convenient way to run some commands on a remote computer once an SSH connection has been established. If you have configured SSH keys between the two computers, the login procedure will be fully automatic. In this quick and dirty example, you will be prompted for the password for the user account on the remote computer.

It's script es “heredoc-8.sh”. We are going to connect to a remote computer called “pc-remota”. The user account is called “dave”. We are using the -T (disable pseudo-terminal mapping) because we don't need to be assigned an interactive pseudo-terminal.

In the section “do some work here” del script, we could pass a list of commands, and these would run on the remote computer. Of course, you could just call a script that was on the remote computer. The remote script could contain all the commands and routines you want to run.

All that our script, heredoc-8.sh, going to do is update a connection record on the remote computer. The user account and the date and time stamp are recorded in a text file.

#!/bin/bash

ssh -T [email protected] << _remote_commands

# do some work in here

# update connection log
echo $USER "-" $(date) >> /home/dave/conn_log/script.log
_remote_commands

When we run the command, we are asked for the password of the account in the remote computer.

./heredoc-8.sh

Some information about the remote computer is displayed and we return to the command prompt.

About him remote computer, we can use cat to check connection log:

cat conn_log/script.log

Every connection is listed for us.

RELATED: How to create and install SSH keys from the Linux Shell

strange name, cool features

Here the documents are extravagant but powerful, especially when used to send commands to a remote computer. It would be very easy to script a backup routine using rsync. Subsequently, the script could connect to the remote computer, check remaining storage space and send email alert if space was running low.

RELATED: How to Backup Your Linux System

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.