What is/are the EOF in ubuntu and also about Kernighan and Ritchie
Actually, in Ubuntu, which uses the bash shell, typing ^Z runs the shell’s suspend command which simply puts the running program in the background. It’s still running, though. So ^Z really has nothing to do with EOF in Ubuntu. You can see this if you add e.g. a call to printf() after the loop in your program.
3 Answers 3
Two different things are happening.
Typing Control-Z in Windows or Control-D in Linux triggers an end-of-file condition and causes getchar() to return the value EOF .
Typing Control-Z in Linux does something different: it suspends the execution of your program. After typing Control-Z, if you type the jobs command, it will show your suspended program. Typing fg will cause it to resume and continue accepting input — until you signal an end-of-file condition by typing Control-D. That would be easier to see if your program generated some output.
The behavior of Control-Z isn’t connected to the way your program is written; Control-Z will suspend (nearly) any running program, whether it’s written in C or not, and whether it’s waiting for input or not. That behavior is specific to Linux and other Unix-like operating systems.
You’ll probably never run out of doubts and questions; I certainly haven’t.
@Keith : Thank you for your time and response. Sir, your answer has really helped a lot.So it means that there is only one EOF in linux and that is ctrl+d.Your answer also sums up why I was taken back into the program itself instead of the source code when I used fg with ctrl+z. Thank you very much for the tip on English as well .
@Ravi.y: It’s more indirect than that. Control-D is not EOF . Typing Control-D when a program is waiting for input triggers an end-of-file condition, which causes the getchar() function to return the special value EOF (which is typically equal to -1 ). An I/O error also causes getchar() to return EOF ; you can distinguish between end-of-file and an error (after gethchar() has returned EOF ) using the feof() and ferror() functions. Yes, Control-D is the only input in Linux that triggers an end-of-file condition. (You can use stty to reconfigure that — but don’t.)
I went through that link but still couldn’t decide whether the usage is incorrect.My personal opinion is that we learn English to communicate with people around the world.Now if our usage creates ambiguity then it would be appropriate for us to correct it.Here in India , the word «doubt» is used for «uncertainty\question» and its widely(99.9%) used. Its always good of people like you to point out such things. I have edited this question and will keep in mind the difference between «doubt» and «question». Thank you again, Sir.
@Ravi.y: I’ve removed the last paragraph of my answer and added it as a comment, since it’s not directly relevant to what you’re asking.
- CTRL+Z in Windows and CTRL+D in Linux sends EOF value to stdin, EOF itself is implementation defined. Only CTRL+D in linux works. You can print it like printf(«%d», EOF); to see its numerical value.
- Yes, doubts and thoughts always useful =)
@Jlghtuse:thank you for your response and time , can you please tell me what does ctrl+z do then ? is not an eof in linux? because the program still stopped when i used it. isn’t thats what an EOF supposed to do ?
CTRL-Z causes a process to suspend in linux. It basically pauses your program and sends you back to the command line. But the program has not completed. If you type fg at the prompt, it will resume, without having seen an EOF.
@Ravi.y, Ctrl+Z is just a key combination. In Windows it sends to program EOF, in Linux it stops process as AShelly says. Key combination for sending EOF in Linux is Ctrl+D.
EOF in C is a condition(«End of File), not a special character telling you that you have reached the end — there is no more input. This condition is indicated to you by various functions such as getchar() returning the special value EOF. EOF is just a value (often a macro that is #defined to be -1), that otherwise cannot occur in the data you read by these functions.
If you read from a file, or a pipe, you know you’ve reached the end of that file when getchar() returns EOF. getchar() reads from stdin. You can have a pipe connected to stdin instead of your console by running your program as echo hello | ./theprogram
If you read input from a terminal/console, things are more complex. On some system there are huge layers involved in bringing input from your keyboard into your program.
But the short version is that on some systems, when you hit the CTRL+D (or CTRL+Z on others) key combination, it’s able to notify your program that the end of input is reached, which makes getchar() return EOF.
As for CTRL+z on linux, this would suspend(halt, but not terminate) the current foreground process.
What Is Cat EOF in Bash Script
CAT is a command of Bash in the Linux operating system. CAT is the abbreviation of concentrate. In the Linux operating system, the cat command is used to display the file, read a file, or concentrate the content of the file as the name explains. It takes a file, reads its content or data, and then outputs the content of the files. It also helps us with the creation of files. This command comes with many options that help us to perform the actions with the file according to our needs.
The here-document is taken as a single word that starts after the line is ended and a new line is started. It keeps going on until the new line has an empty string or blank character in it. To some extent, there may be multiple line spacing between two paragraphs or some special characters. It becomes difficult for the simple echo command to get to the end of the file. Maybe it terminates the execution of the file if we pass the EOF term along with the cat command. It terminates when the end of file is reached.
Example:
In this example, we first print the content of the file and then copy the content to another one. To do so, we first create a Bash script in which we store some content which is then copied to the other file. Let us first create a new Bash file. We can simply create the Bash file by writing the command or by simply using the notepad. In this example, we create a new Bash file using the command.
To create the new file, we write the following command:
In the previously-mentioned command, we create and open the “bash.sh” file using the “nano” text editor. As we can see in the following illustration, the file named “bash.sh” is opened while pressing enter. After we add some content to the file, we then print it to the other file. In this file, the content is added between the “cat
As we can see in the following snippet, our file is successfully created and saved in our home directory. One thing to remember is when we don’t add the path while creating a new file, it is automatically saved in the home directory. If we want to store it in the desired location, we can pass the path along with it. The Bash file always contains the “.sh” extension but the file name can be of your choice.
Now, let us display the data from the file that we added to it. To print the data, we simply write the following command:
The Bash command along with the file having the “.sh” extension is passed to it. It means that it prints the content of the Bash file. After running this command by pressing “enter”, we get the following output in which the content that is written inside the “cat
Now, we try to see what happens if we add “EOF” between the paragraphs, whether it prints the whole file or not. In the following snippet, we add the “EOF” command after the first line. Now, using the Bash command, we print the Bash file again.
As we can see, it just displays the first line which is the “ this is my first EOF” command. It did not display the content that was written after that command. Instead, it displays the error message that the “command is not found”. This means that when we try to print the content from one file to the other, it only adds the content that is inside the “cat EOF” command. The other content is ignored.
Now, let us print the same file to the other file. For that, we first create a variable inside the Bash file that we created named “bash.sh”. We assign the path to this variable where the new file is created to which the content and the name of the file are copied. Let’s suppose we named it “bashcopy.txt”. This means that we want to copy the content of the Bash in which we declare as a variable named “myvar”. By assigning it, we create the path and the name of the file with a text file with “.txt” extension. Then, after writing the “cat
In the previous snippet, we modify our “bash.sh” file. Now, let us check for the “bashcopy.txt” file that our content is copied to it successfully or not. For that, we first execute the “bash.sh” file using the Bash command.
This time, nothing is printed while we execute the Bash file. This means that our content is successfully copied to the new text file. Let us check. For that, we can simply check the home directory. We can also access it by just writing the command.
Now, we can see the file named “bashcopy.txt has been successfully created” along with the content that we printed in it using the “myvar” variable.
Conclusion
Today, we studied the cat EOF command. We also gone through the working of cat EOF and the method to use it in the Bash script. After introducing it to you, we apply this with an example to make it easier to understand. We create the new Bash file, printed its data in the terminal, and then printed it to another file.
About the author
Omar Farooq
Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.
Cat EOF
Cat is one of the most common and useful command-line utilities in the Linux ecosystem. It allows you to read or concatenate the contents of a file in a simple and easy to navigate manner. When combined with other tools such as awk, sed, less and more, cat can be one of the most powerful tools in your Linux arsenal.
In this article, we will explore how we can use the cat command and the EOF feature to perform tasks such as writing multi-line strings to a file and more.
Writing Multi-Line String to a File
The cat command and the EOF feature provides us with a simple and intuitive way to writing multi-line strings to a file in Bash. You start by the cat command followed by a left shift operator.
Next, we add the keyword that will force Bash to terminate the input when encountered.
Finally, we pass a single redirection operator and the name of the file you wish to write.
An example syntax is as shown in the following:
Once Bash encounters the keyword EOF in the input, it terminates the input operation and write the provided contents to the file.
An example is as shown in the following:
In the previous example, we start by opening the multi-line operation and tell Bash to write the contents to the echo.sh file. This creates the file if it does not exist.
Finally, we write the multiple strings consecutively and press enter to proceed to the next line.
Once done, we can call the EOF string to terminate the input and write to file. We can verify by viewing the contents of the file.
Using Cat EOF to Pipe Multi-Line String
When working with texts in Bash, you will encounter scenarios where you need to pipe a multi-line string to a command.
The cat and EOF feature can come in handy. In the following example, we use the cat and EOF command to pipe the multi-line string to grep.
The previous command takes all the input strings and pipe each one of them to grep. Grep then searches for matching strings and write them to apache.txt file.
The resulting output is as shown in the following:
Conclusion
In this article, we covered two main methods of using the cat command and EOF feature to write the multi-line string to a file and pipe the multi-line strings in Bash.
About the author
John Otieno
My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list