Published: July 22, 2026
Read: 8 min
In: Technology & Development

Written by

Reviewed by

Last updated: May 1, 2024

Expert verified

SVG Image

TL;DR

Here is how to use Cat EOF for multi-line strings in bash:

  1. Directly write multi-line text to a file using the cat << EOF > output.txt command sequence, ideal for creating configurations or documentation.
  2. Store multi-line text in a variable named my_variable using the read -r -d '' command, facilitating reuse and script clarity.
  3. Dynamically incorporate Bash variables into multi-line text with cat << EOF, personalizing scripts or configuration files.
  4. Generate repeated blocks of text with variations in a loop using cat << EOF, useful for bulk creation of similar configurations or files.
  5. Quickly create and make executable Bash scripts or code templates directly from the command line using cat << EOF > my_script.sh.

Explore the guide below to learn five different methods to use cae EOF for multi-line strings in bash and the best practices for using cat EOF.

Managing multi-line strings in Bash scripting can be tricky, especially when you’re working with complex configurations or automating scripts. Fortunately, using cat EOF is a powerful way to simplify these challenges. This method is excellent for handling large amounts of string data efficiently. In this post, I’ll show you five practical ways to use cat EOF for various tasks—from embedding dynamic content to building detailed scripts. I’ll also share some key best practices to keep your scripts robust and easy to read.

How to Use Cat EOF for Multi-line Strings in Bash?

To use cat EOF for multi-line strings in Bash, start by typing cat <<EOF followed by pressing Enter. Next, type each line of your multi-line string. Once all lines have been entered, conclude by typing EOF on a new line and pressing Enter again. This syntax tells the shell to read everything until it encounters the ending EOF as part of the multi-line string. This method is particularly useful for scripts where embedding complex multi-line text directly into the code is necessary.

Now let’s explore several effective methods to utilize cat EOF in your Bash scripts. Each method is designed to cater to different needs, from writing direct text to files to embedding dynamic variables and more:

1. Writing Multi-Line Text to a File

This method is used to write blocks of multi-line text directly into a file from the command line, useful for creating configuration files, scripts, or documenting information within a project. Follow these steps:

  1. Open your terminal. Make sure you have access to a Bash shell.
open terminal
  1. Enter the command below, substituting output.txt with your desired file name:

cat << EOF > output.txt

This is line one

This is line two

This is line three

EOF

This command directs the text between << EOF and EOF into output.txt.

stroring multi line strings in a text file using EOF
  1. Check the file content by typing:

cat output.txt

This command displays the contents of output.txt to ensure your text has been written correctly.

checking content of file using cat command

2. Storing Multi-Line String in a Variable

Store multi-line strings in a variable for reuse throughout your script, enhancing script clarity and reducing duplication. Here is how to do it:

  1. Access your command window and run the following script to store the multi-line string in a variable named my_variable:

read -r -d ' ' my_variable << 'EOF' line 1 line 2 line 3 EOF

This script uses the read command to capture the multi-line text into my_variable until it encounters an EOF marker.

stroring multi line strings in a variable using EOF
  1. Display the variable by entering:

echo "$my_variable"

This command prints the contents of my_variable to verify that it stores the correct string.

verifying the variable content

3. Embedding Variables within the Text

Dynamically insert Bash variables into your multi-line text, allowing for personalized scripts or configurations. Here is the step-by-step guide to do it:

  1. Launch your Terminal and define the variable you want to use, e.g.:

username="John"

This command assigns the value "John" to the variable username.

defining a variable and assigning its value
  1. Use the following cat EOF syntax to embed the variable within the multi-line text:

cat << EOF
Hello, $username!
Welcome to the system.
EOF

This script will dynamically insert the value of the username into the text at the specified location.

embedding the variable in string

4. Using cat EOF in Loops

Generate repeated blocks of text with variations, useful for creating multiple configuration files or similar environments in bulk. Here are the steps to do it:

  1. In your Terminal type the following loop script:

for i in {1..5}; do
cat << EOF
This is iteration $i
Another line
EOF
done

This loop uses cat EOF to generate a text block for each iteration, inserting the loop counter $i into each block.

writing loop script using cat EOF
  1. Execute the script and observe the output, demonstrating the effect of the loop on the generated text.
output

5. Creating Scripts or Code Blocks

Quickly generate executable scripts or code templates directly from the command line, streamlining development processes and automation tasks. Follow these steps:

  1. Open your command prompt and use cat EOF to write a new script to a file:

cat << EOF > my_script.sh #!/bin/bash echo "Starting the script..." # Add more commands here echo "Script ended." EOF

This command writes a complete bash script to my_script.sh.

using cat EOF to write a script to a file
  1. Make the script executable by running:

chmod +x my_script.sh

This command changes the permissions of my_script.sh, making it executable.

making script file executable
  1. Verify the script by executing:

./my_script.sh

This command runs the script, allowing you to verify its functionality.

executing the script file

5 Best Practices for Using cat EOF in Bash Scripting

When using cat EOF for handling multi-line strings in Linux Bash, following best practices can greatly improve the efficiency, security, and readability of your scripts. Here are five key tips to help you use this feature effectively:

  • 📝 Use Indentation Consistently: Always keep the lines between your EOF markers indented at the same level. This uniformity makes your scripts easier to read and understand. Be consistent with your choice of tabs or spaces to prevent issues when running scripts on different systems or editors.
  • 🔒 Escape Variables When Needed: If you don’t want Bash to replace variables with their values inside the EOF block, surround EOF with single quotes, like EOF. This tells Bash to treat everything inside as plain text, not as code or variables. It’s useful when you want to show examples in your script or store exact text.
  • 🚀 Avoid Using Reserved Words as Delimiters: Choose a unique word for your EOF delimiter to prevent confusion with words Bash already uses (like for, done, or fi). Using common words can lead to errors where Bash misinterprets your intentions. 
  • 🛡️ Secure Input Data: Always check and clean data from outside your script before using it in an EOF block. This is crucial to prevent harmful code from sneaking into your script, which can happen if you include user input or data from the internet. Use tools or methods to sanitize this data first.
  • 🧹 Close EOF Properly: Make sure the EOF delimiter has no extra spaces or characters before or after it. This precise placement ensures that your script recognizes the end of the text block correctly. Mistakes here can cause parts of your script not to run or errors to appear.

In a Nutshell

In this article, I’ve shown you five methods to use cat EOF for handling multi-line strings in Bash. From creating simple text files to embedding variables dynamically in scripts, these techniques form the foundation for effective Bash scripting. By sticking to the best practices I discussed, you’ll make your scripts not only functional but also secure and easy to manage.

If you’re keen to take your Bash scripting skills further, consider exploring topics such as how to echo a new line in Bash Shell Scripts to manage script outputs better, enhance your command knowledge by learning to use the Help command, and master string manipulation through learning how to replace a string in Bash. These will directly enhance and build upon your existing scripting abilities.

  1. Can cat EOF be used with other shell environments, like Zsh or Ksh?

    Yes, cat EOF can be used in other shell environments such as Zsh and Ksh. The syntax and functionality remain largely consistent across these shells, making it a versatile tool for scripting across different Unix-like systems.

  2. Is there a performance impact when using cat EOF with large blocks of text?

    Using cat EOF with very large blocks of text can impact performance, particularly in terms of processing speed and memory usage. However, for most typical script usage, this impact is minimal. It’s more noticeable in scenarios where scripts process large data volumes repetitively or in constrained environments.

  3. How do you handle special characters or escape sequences in a cat EOF block?

    In a cat EOF block, special characters and escape sequences can be managed by using single quotes around the EOF delimiter (‘EOF’). This method treats the content as a literal string, preventing the shell from interpreting special characters or escape sequences.

  4. Can cat EOF be used to generate formatted output, such as JSON or XML?

    Yes, cat EOF can be effectively used to generate formatted outputs like JSON or XML. By carefully structuring the content within the EOF block, you can create complex data formats. Ensure proper indentation and syntax to match the requirements of JSON or XML for successful integration with other systems.