How do I use: cat << EOF
61,030
The <<
is the heredoc. Anything that follows the <<
acts as the delimiter for the heredoc
cat <<EOF
This is first line
So, this is 2nd
Again, another line
EOF
Enter
So if you would like to store the three lines to a variable do it like below :
from the command line
$ var="$(cat <<EOF
> Bingo
> Gotcha
> Enough
> EOF
> )"
Enter
$ echo "$var" #double quotes are important
Bingo
Gotcha
Enough
from inside the script
#!/bin/bash
var="$(cat <<EOF
Bingo
Gotcha
Enough
EOF
)"
echo "$var"
Related videos on Youtube
Author by
Mayur Narang
Updated on September 18, 2022Comments
-
Mayur Narang over 1 year
I am little confused and not sure, how to add the cat << EOF style to my script. Would really appreciate a little feedback and help.
Here is my script
!/bin/sh #This is a disk mailer script for hdfs DU_RESULTS_HOME=$(sudo -u hdfs hadoop fs -du -s /user/* | sort -r -k 1 -g | awk '{ suffix="KMGT"; for(i=0; $1>1024 && i < length(suffix); i++) $1/=1024; print int($1) substr(suffix, i, 1), $3; }'| head -n 22 |awk '{ print $1"\t\t\t" $2 }') DF_RESULTS_HOME=$(sudo -u hdfs hadoop fs -df -h /user) HOSTNAME=$(hostname -s) MESSAGE_SUBJECT="Disk usage" MESSAGE_SENDER="xyz" MESSAGE_EMAIL="abc" DU_RESULTS_HEADER_USER="Dir size User" MESSAGE_BODY="Team: High level Disk Usage Report for: /user Please take a moment to clean out any old data, and write temp files to an appropriate filesystem, such as /tmp. Thanks if [[ --debug == "$1" ]]; then echo debug on printf '%s\n\n\n\n%s\n\n%s\n%s\n\n\n\n\n%s\n%s\n%s' "$MESSAGE_BODY" "$DF_RESULTS_HOME" "$DU_RESULTS_HEADER_USER" "$DU_RESULTS_HOME" else echo debug off printf '%s\n\n\n\n%s\n\n%s\n%s\n\n\n\n\n%s\n%s\n%s' "$MESSAGE_BODY" "$DF_RESULTS_HOME" "$DU_RESULTS_HEADER_USER" "$DU_RESULTS_HOME" | /bin/mail -s "$MESSAGE_SUBJECT" -r "$MESSAGE_SENDER" "$MESSAGE_EMAIL" fi
-
steve almost 8 yearsAs it stands, the script needs a quote
"
afterThanks
, to terminate the string. -
ilkkachu almost 8 years
-
G-Man Says 'Reinstate Monica' almost 8 yearsIf you're going to point people to references, please point them to this one.
-
-
Mayur Narang almost 8 yearsso i can do DU_RESULTS_HOME="$(cat <<EOF sudo -u hdfs hadoop fs -du -s /user/* | sort -r -k 1 -g | awk '{ suffix="KMGT"; for(i=0; $1>1024 && i < length(suffix); i++) $1/=1024; print int($1) substr(suffix, i, 1), $3; }'| head -n 22 |awk '{ print $1"\t\t\t" $2 }') sudo -u hdfs hadoop fs -df -h /user EOF )"
-
sjsam almost 8 years
<<
is meant for strings like I've demonstrated, not commands. Do you want to execute tall the commands and store the results toDU_RESULTS_HOME
? -
Mayur Narang almost 8 yearsno, so in my script i am using so many variables, so i am trying to learn the cat << eof technique to reduce the number of variables.. and probably avoid using print format, which is printf '%s\n\n\n\n%s\n\n%s\n%s\n\n\n\n\n%s\n%s\n%s' "$MESSAGE_BODY" "$DF_RESULTS_HOME" "$DU_RESULTS_HEADER_USER" "$DU_RESULTS_HOME" | /bin/mail -s "$MESSAGE_SUBJECT" -r "$MESSAGE_SENDER" "$MESSAGE_EMAIL"