Skip to content
Ricky
English

Search

Searching… No matching pages. Matching pages are listed below. Search is unavailable. Please try again.

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

  • Ricky Lin
  • 1 minute
On this page

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

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

Disabling pathname/parameter/variable expansion, command substitution, arithmetic expansion with 'EOF'

#!/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..."