How to Read File Into Variable in Bash
- Method 1: Using Command Substitution
-
Method 2: Using the
read
Command in a Loop - Method 3: Using Here Documents
- Conclusion
- FAQ

When working with Bash, one common task is reading the contents of a file into a variable. This can be particularly useful when you need to manipulate or analyze data stored in a text file. Whether you are scripting for automation, system administration, or development purposes, knowing how to read a file into a variable can streamline your workflow.
In this tutorial, we will explore various methods to accomplish this task, focusing on practical examples that you can easily implement. By the end of this guide, you will be equipped with the knowledge to effectively use Bash for reading files into variables, enhancing your scripting capabilities.
Method 1: Using Command Substitution
One of the most straightforward ways to read a file into a variable in Bash is through command substitution. This method allows you to capture the output of a command and assign it to a variable. Here’s how you can do it:
content=$(cat filename.txt)
In this line, we use the cat
command to read the contents of filename.txt
. The dollar sign and parentheses $(...)
indicate command substitution, which captures the output of the command. The contents of the file are then stored in the variable content
.
To check what has been stored in the variable, you can simply echo it:
echo "$content"
Output:
This is the content of the file.
This method is particularly useful when dealing with smaller files. However, be cautious with larger files, as reading everything into a variable can consume a significant amount of memory. If the file size is manageable, command substitution provides a quick and efficient way to access file contents.
Method 2: Using the read
Command in a Loop
Another effective method to read a file into a variable is by using the read
command within a loop. This approach is beneficial when you want to process the file line by line. Here’s how you can implement this:
while IFS= read -r line; do
content="$line"
done < filename.txt
In this code snippet, we use a while
loop to read each line of filename.txt
. The IFS=
sets the Internal Field Separator to an empty value, ensuring that leading/trailing whitespace is preserved. The -r
option prevents backslashes from being interpreted as escape characters. Each line read is stored in the variable content
, which will ultimately hold the last line of the file after the loop completes.
If you want to see all lines, you could modify the script slightly to append each line to an array instead:
lines=()
while IFS= read -r line; do
lines+=("$line")
done < filename.txt
To display all lines stored in the array:
printf "%s\n" "${lines[@]}"
Output:
This is the first line.
This is the second line.
This is the last line.
This method is especially useful for processing files with multiple lines, as it allows you to handle each line individually. You can easily modify the script to perform specific operations on each line, making it a versatile choice for various scripting needs.
Method 3: Using Here Documents
Here Documents (Heredoc) is another technique you can use to read a file into a variable in Bash. This method is particularly useful when you want to include multi-line content directly within your script. Here’s how it works:
content=$(cat <<EOF
This is the first line.
This is the second line.
EOF
)
In this example, we use a Here Document to define a block of text. The EOF
marker indicates the beginning and end of the content. The cat
command reads this block, and the output is captured into the variable content
.
To display the content stored in the variable, you can use:
echo "$content"
Output:
This is the first line.
This is the second line.
Heredocs are particularly handy when you want to include multi-line strings directly in your script without needing an external file. This method enhances readability and allows for easy adjustments to the text content.
Conclusion
Reading a file into a variable in Bash is a fundamental skill that can significantly improve your scripting efficiency. Whether you choose command substitution, the read command in a loop, or Here Documents, each method has its unique advantages. Depending on your specific needs, you can select the approach that best fits your task. By mastering these techniques, you’ll be well-equipped to handle various file manipulation tasks in your Bash scripts, making your programming experience smoother and more productive.
FAQ
-
How can I read a file into a variable without using the
cat
command?
You can use theread
command within a loop to read each line of the file directly withoutcat
. -
What should I do if the file is too large to fit into a variable?
Consider processing the file line by line using a loop to avoid memory issues. -
Can I read multiple files into a single variable?
Yes, you can concatenate multiple files usingcat
and then read them into a variable. -
Is there a way to read only specific lines from a file?
You can use tools likesed
orawk
to extract specific lines and then read them into a variable. -
How do I handle special characters in the file content?
Using the-r
option with theread
command can help preserve special characters without interpretation.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn