Blog

03/06/2019

How to use a here documents to write data to a file in bash script



How do I use a heredoc redirection feature (here documents) to write data to a file in my bash shell scripts?

A here document is nothing but I/O redirection that tells the bash shell to read input from the current source until a line containing only delimiter is seen.

This is useful for providing commands to ftp, cat, echo, ssh and many other useful Linux/Unix commands. This feature should work with bash or Bourne/Korn/POSIX shell too.

heredoc syntax

The syntax is:

command <<EOF
cmd1
cmd2 arg1
EOF

command <<EOF cmd1 cmd2 arg1 EOF

OR allow here-documents within shell scripts to be indented in a natural fashion using EOF<<-

command <<-EOF
  msg1
  msg2 
 $var on line 
EOF

command <<-EOF msg1 msg2 $var on line EOF

OR

command <<'EOF'
 cmd1
 cmd2 arg1
 $var won't expand as parameter substitution turned off
 by single quoting 
EOF

command <<‘EOF’ cmd1 cmd2 arg1 $var won’t expand as parameter substitution turned off by single quoting EOF

OR redirect and overwrite it to a file named my_output_file.txt:

command <<EOF > my_output_file.txt
 mesg1
 msg2
 msg3
 $var on $foo
EOF

command <<EOF > my_output_file.txt mesg1 msg2 msg3 $var on $foo EOF

OR redirect and append it to a file named my_output_file.txt:

command <<EOF >> my_output_file.txt
 mesg1
 msg2
 msg3
 $var on $foo
EOF

command <<EOF >> my_output_file.txt mesg1 msg2 msg3 $var on $foo EOF

Examples

The following script will write the needed contents to a file named /tmp/output.txt:

#!/bin/bash
OUT=/tmp/output.txt
 
echo "Starting my script..."
echo "Doing something..."
 
cat <<EOF >$OUT
  Status of backup as on $(date)
  Backing up files $HOME and /etc/
EOF
 
echo "Starting backup using rsync..."

#!/bin/bash OUT=/tmp/output.txt echo "Starting my script…" echo "Doing something…" cat <<EOF >$OUT Status of backup as on $(date) Backing up files $HOME and /etc/ EOF echo "Starting backup using rsync…"

You can view /tmp/output.txt with the cat command:
$ cat /tmp/output.txt
Sample outputs:

  Status of backup as on Thu Nov 16 17:00:21 IST 2017
  Backing up files /home/vivek and /etc/

Disabling pathname/parameter/variable expansion, command substitution, arithmetic expansion

Variable such as $HOME and command such as $(date) were interpreted substitution in script. To disable it use single quotes with ‘EOF’ as follows:

#!/bin/bash
OUT=/tmp/output.txt
 
echo "Starting my script..."
echo "Doing something..."
# No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word.  
# If any part of word is quoted, the delimiter  is  the  result  of  quote removal  on word, and the lines in the here-document 
# are not expanded. So EOF is quoted as follows
cat <<'EOF' >$OUT
  Status of backup as on $(date)
  Backing up files $HOME and /etc/
EOF
 
echo "Starting backup using rsync..."

#!/bin/bash OUT=/tmp/output.txt echo "Starting my script…" echo "Doing something…" # No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. # If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document # are not expanded. So EOF is quoted as follows cat <<‘EOF’ >$OUT Status of backup as on $(date) Backing up files $HOME and /etc/ EOF echo "Starting backup using rsync…"

You can view /tmp/output.txt with the cat command:
$ cat /tmp/output.txt
Sample outputs:

  Status of backup as on $(date)
  Backing up files $HOME and /etc/

A note about using tee command

The syntax is:

tee /tmp/filename <<EOF >/dev/null
line 1
line 2
line 3
$(cmd)
$var on $foo
EOF

tee /tmp/filename <<EOF >/dev/null line 1 line 2 line 3 $(cmd) $var on $foo EOF

Or disable variable substitution/command substitution by quoting EOF in a single quote:

tee /tmp/filename <<'EOF' >/dev/null
line 1
line 2
line 3
$(cmd)
$var on $foo
EOF

tee /tmp/filename <<‘EOF’ >/dev/null line 1 line 2 line 3 $(cmd) $var on $foo EOF

Here is my updated script:

#!/bin/bash
OUT=/tmp/output.txt
 
echo "Starting my script..."
echo "Doing something..."
 
tee $OUT <<EOF >/dev/null
  Status of backup as on $(date)
  Backing up files $HOME and /etc/
EOF
 
echo "Starting backup using rsync..."

#!/bin/bash OUT=/tmp/output.txt echo "Starting my script…" echo "Doing something…" tee $OUT <<EOF >/dev/null Status of backup as on $(date) Backing up files $HOME and /etc/ EOF echo "Starting backup using rsync…"

A note about using in-memory here-docs

Here is my updated script:

#!/bin/bash
OUT=/tmp/output.txt
 
## in memory here docs 
## thanks https://twitter.com/freebsdfrau
exec 9<<EOF
  Status of backup as on $(date)
  Backing up files $HOME and /etc/
EOF
 
## continue
echo "Starting my script..."
echo "Doing something..."
 
## do it
cat <&9 >$OUT
 
echo "Starting backup using rsync..."

#!/bin/bash OUT=/tmp/output.txt ## in memory here docs ## thanks https://twitter.com/freebsdfrau exec 9<<EOF Status of backup as on $(date) Backing up files $HOME and /etc/ EOF ## continue echo "Starting my script…" echo "Doing something…" ## do it cat <&9 >$OUT echo "Starting backup using rsync…"

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...