Understanding reading from a file

Let's write a script to read from a file:

Write the script file_02.sh, shown as follows:

#!/bin/bash 
# We will open file sample_input.txt for reading purpose. 
# We are assigning descriptor 3 to the file. 
exec 3< sample_input.txt 
 
cat <&3 
# Closing file 
exec 3<&- 

Save the file, give the permission to execute, and run the script as follows:

    $ chmod u+x file_02..sh
  

We will create the sample_input.txt file as follows:

    $ echo "Hello to All" > sample_input.txt
  

Run the script and check the result:

    $ ./file_02.sh
  

This should produce the following output:

    Hello to All
  

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.