Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash Heredoc - DesignLinux

Nov 12 2020
designlinux 0 Comments
Bash Heredoc

A Heredoc (Here document) is a redirection type which allows to pass multiline lines of input to a command. Sometimes while writing shell scripts you need to pass multiline block of code or text with the command like cat, sftp or tee. In this guide, you will learn how to use the Heredoc in bash.

Following is the basic syntax for the HereDoc:

[COMMAND] <<[-] 'DELIMITER'
  HERE-DOCUMENT
DELIMITER

Here,

  • It starts with optional command followed by the redirection operator << and delimiter in first line.
    • The most commonly used delimiting identifiers are EOF or END. You can use any string for it.
    • If the delimiting identifier is unquoted, the shell will substitute all variables, commands and special characters before passing the here-document lines to the command.
    • Use the minus sign to the redirection operator <<- to ignore the all leading tab characters. It will maintain proper indentation.
  • You can give variables, strings, command or any other type of input in the here-document block.
  • The last line ends with the delimiting identifier. White space in front of the delimiter is not allowed.

Basic Heredoc Examples#

For better understanding let’s see few examples of heredoc. Mostly, heredoc is used in combination with the cat command.

Below, we are passing multiple lines which print the home directory path using an environment variable and cat command with here document:

cat << EOF
The home directory is: $HOME
You are logged in as: $(whoami)
EOF

You will get the following output:

The home directory is: /home/tecnstuff
You are logged in as: tecnstuff

In output both the variable and the command output are substituted.

Another example, in which we will enclose the delimiter in single or double quotes:

cat <<- "EOF"
The home directory is: $HOME
You are logged in as: $(whoami)
EOF

You will get he following output and you can see that when the delimiter is quoted, shell will not command substitution.

The current working directory is: $HOME
You are logged in as: $(whoami)

When you are using a heredoc inside a statement or loop, use the <<- redirection operation that allows you to indent your code.

if true; then
    cat <<- EOF
    Line with indent.
    EOF
fi
Line with indent.

You also can redirect the output to a file instead of displaying on a screen using the >, >> operators.

cat << EOF > output.txt
The home directory is: $HOME
You are logged in as: $(whoami)
EOF

If you will use the > operator, it will overwrite the file and >> will append the output to the file. If the given file output.txt doesn’t exist it will be created.

Using Heredoc with SSH#

On the remote system over SSH, you can run multiline command using the Heredoc easily.

Ensure that you have escape all commands, variables and special characters while using without quotes, otherwise they will be
execute locally:

ssh -T username@ip_or_hostname << EOF
echo "The local home directory is: $HOME"
echo "The remote home directory is: \$HOME"
EOF
The local home directory is: /home/tecnstuff
The remote home directory is: /home/username

Visit our guide to set up an SSH key-based authentication to connect your Linux servers without entering a password.

Conclusion#

You have learned basics of the heredoc and use it in your bash scripts.

If you have any questions or feedback, feel free to leave a comment.

Related

Tags: bash, terminal

How to Create Simple Shell Scripts in Linux

Prev Post

How to Work with Date and Time in Bash Using date Command

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 55
  • 1,054,772

DesignLinux.com © All rights reserved

Go to mobile version