Join Scott Simpson for an in-depth discussion in this video Reading and writing text files, part of Learning Bash Scripting.
…Eventually, you'll probably want to work with files.…If you want to work with content of binary files, you're on your own for that.…That's a territory better served by C or something a bit lower level.…But working with text files in Bash is pretty easy.…The greater than and less than symbols are key here.…For example, if I write, echo, "Some text" > file.txt,…bash writes the string, Some text, to a file called file.txt.…Creating it if it doesn't and overwriting anything that's already in it.…You're probably familiar with this from working with output redirection.…
You can use the greater than operator by itself to…zero out a file, getting rid of anything that's inside it.…But let's put something back in there.…If you want to add onto the end of a file…rather than replace it, you can use two greater than symbols.…So what about reading files?…Well, for that, you can use a while loop, along with a read command.…This syntax reads the file, file.txt, into the variable f line by line.…To see this in action, I'll add an…
Author
Released
11/26/2013- What is Bash?
- Managing output with grep, awk, and cut
- Understanding Bash script syntax
- Creating a basic Bash script
- Displaying text with "echo"
- Working with numbers, strings, and arrays
- Reading and writing text files
- Working with loops
- Using functions
- Getting user input during execution
- Ensuring a response
Skill Level Beginner
Duration
Views
Related Courses
-
Unix for Mac OS X Users
with Kevin Skoglund6h 35m Beginner
-
Introduction
-
Welcome1m 1s
-
-
1. Working with the Command Line
-
What's Bash?2m 27s
-
Tilde and brace expansion3m 49s
-
Creating a basic Bash script1m 52s
-
-
2. Building Bash Scripts
-
Displaying text with echo1m 32s
-
Working with variables2m 32s
-
Command substitution1m 9s
-
Working with numbers2m 21s
-
Comparing values3m 13s
-
Working with strings3m 9s
-
Coloring and styling text6m 15s
-
Working with arrays2m 35s
-
Using here documents1m 43s
-
-
3. Control Structures
-
Introducing for loops3m 13s
-
Using functions2m 25s
-
4. Interacting with the User
-
Working with arguments1m 51s
-
Working with flags2m 18s
-
Ensuring a response3m 15s
-
-
Conclusion
-
Next steps48s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Reading and writing text files