What is a here-document in Linux?

0222

A here-document (or heredoc) in Linux is a type of redirection that allows you to pass a block of text to a command or script directly within the script itself. It is often used in shell scripting to provide multi-line input to commands without needing to create a separate file.

The syntax for a here-document is as follows:

command <<EOF
line 1
line 2
line 3
EOF

In this example:

  • command is the command that will receive the input.
  • <<EOF indicates the start of the here-document, where EOF is a delimiter that can be any string (commonly EOF).
  • The lines between the start and end delimiters are the input that will be passed to the command.

For example, you can use a here-document with the cat command to create a file:

cat <<EOF > myfile.txt
This is line 1.
This is line 2.
This is line 3.
EOF

This will create a file named myfile.txt containing the specified lines. Here-documents are useful for embedding large blocks of text or configuration data directly in scripts.

0 Comments

no data
Be the first to share your comment!