An EOF (end of file) condition is used to indicate the end of a file or data stream. It is a marker that tells the operating system it has reached the end of the data which it is reading. A Linux system utilizes an EOF marker whenever it is reading data, and users can also manually specify an EOF with various Linux commands like
cat. In this tutorial, we will explain EOF in Linux and see examples of how to utilize it ourselves via the Heredoc function in Bash.
In this tutorial you will learn:
- What is EOF used for in Linux?
- EOF example with
Ctrl + Dkey press
- EOF example with heredoc function in Bash
|Requirements, Conventions or Software Version Used
|Any Linux distro
|Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
What is EOF used for in Linux?
The EOF marker is a character that follows a file or data stream. The character is usually invisible in the system terminal, so you have likely encountered EOF markers many times without realizing that there was a character which is telling the program it has reached the end of the data.
EOF markers are not a concept only found in Linux, but are commonly used throughout other operating systems and programs or scripts written in many different languages. If a script is prompting the user for input, it will often listen for an EOF marker so it can know when to stop expecting more input.
EOF examples in Linux
In case the concept of EOF markers is still hard to grasp, we can take advantage of EOF indicators in several popular Bash commands. Let’s see some examples.
- The EOF character can be entered with the
Ctrl + Dkey combination. Let’s look at an example of using this with the cat command. When the
catcommand does not contain any arguments, it waits for an input from your keyboard. If you try to run the
catcommand lacking any arguments,
catwill wait for your input from the keyboard until it receives an end-of-file ( EOF ) signal produced by
CTRL+Dkey combination. When entering some input from a keyboard,
catcommand will simply repeat any input and display it on the screen.
$ cat Some example test [Here we press the Ctrl + D combo] Some example text
- This keyboard input can be redirected directly into a file with the
>operator. The following example illustrates this idea:
$ cat > file Some example text [Ctrl + D] $ cat file Some example text
After entering the “Some example text” line,
ENTERkey was pressed to produce a new line character, followed by
CTRL+D, which produces an end-of-file signal. Reading the content of file with the
catcommand confirms that the input from the keyboard was undeniably redirected into the file.
- Another way we can use EOF is by using the Heredoc functionality of Bash. This allows you to pass multiple lines into a file. You need to specify a “delimiter” at the beginning of your command, which is normally just EOF (end of file) but can technically be any characters. Here’s what it would look like if we were appending three lines into a file.
cat << EOF >> file.txt My user account is: $(whoami) My home directory is: $HOME Pretty cool, huh? EOF
As you can see, we manually typed out ‘EOF’ to indicate to Heredoc that we have reached the end of the data stream. And the results…
$ cat file.txt My user account is: linuxconfig My home directory is: /home/linuxconfig Pretty cool, huh?
In this tutorial, we explained how EOF (end of file) is used on a Linux system. EOF is an essential concept used by many operating systems and programs in order to know when the end of a file has occured or when no more input is to be expected. As Linux users, we can use the
Ctrl + D function to specify the EOF ourselves or use other representations to pass an EOf to a program.