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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.