1

What are the ============= in the below program? Is that section needed or is it a comment block and can be taken out? I'm somewhat new to UNIX so I am trying to read code to understand it and don't know why this is like this. Also, what is the EOF below it mean. I'm not sure about the meaning of that line either. Can anyone explain please? Thanks in advance

#!/bin/bash    

usage() {
    cat <<-EOF
    ========================================================
    Usage: $0

    Choose either y or n in "do you want to continue"

    Choose from option A - E in mainmenu to perform actions.
    ========================================================

    EOF
}
DavidScherer
  • 863
  • 1
  • 14
  • 26
gggeorge
  • 11
  • 2

1 Answers1

3

This question "How does “cat << EOF” work in bash?" explains how cat can be used for "here documents".

This question on How can I write a heredoc to a file in Bash script? also has detailed answers on specifically using cat for this and contains this example that writes the content to a file:

cat << EOF > /tmp/yourfilehere
These contents will be written to the file.
        This line is indented.
EOF

From the Advanced Bash-Scripting Guide section on here-docs:

A here document is a special-purpose code block. It uses a form of I/O redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor.

In this case, the ======================================================== is simply text content to be displayed.

Looking at your code, the heredoc is defined within a method called usage that appears to be called from error_exit(), so I would imagine it's there to display a message about the use of the script to users that input incorrect options.

EOF is an "end of file" - think of it as the beginning and end of the heredoc content.

In answer to your question whether the ======================================================== can be taken out - yes, it can. All that will happen is that it will no longer be displayed to the user. However, don't remove the EOF!

Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35