Reading Files Without "cat"

while IFS= read -r line; do echo "$line"; done < filename.txt

In this command:

  • while IFS= read -r line;: This loop reads the file line by line. IFS= (Internal Field Separator) ensures that whitespace at the beginning and end of lines is preserved. The -r prevents backslash characters from being interpreted as escape characters.
  • do echo "$line";: For each line read from the file, echo is used to print the line to the terminal.
  • < filename.txt: Redirects the contents of the file filename.txt to the while loop.

This command can be run directly in the terminal to read and display the contents of a specified .txt file.