From the course: Learning Bash Scripting

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Reading and writing text files

Reading and writing text files - Bash Tutorial

From the course: Learning Bash Scripting

Reading and writing text files

To write to text files from a Bash script, we'll use output redirection, which we saw briefly earlier in the course. The two primary operators we'll use here are the single and double greater than signs. The single greater than sign stands for redirection, where the existing information in the destination file gets replaced with the new information from whatever operation is writing the information, and the double greater than signs represent the existing information in the destination file being preserved, with new information added or appended to the end of the file. To read from files, we'll use the input redirection operator to use a text file as input for the read keyword with a while loop, which will read the text file line by line, giving us the opportunity to use each line in turn within the while loop. Let's write a basic script that will output a few lines of text to a file. I'll open my script and clean it out. I'll write a for loop with for i in 1, 2, 3, 4, 5. And for each…

Contents