Assigns the file descriptor (fd) to file for input

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← Assigns the file descriptor (fd) to file for outputHomeCloses the file descriptor (fd) →

To assign a file descriptor to an input file use the following syntax:

exec fd< input.txt
  • where, fd >= 3.

Example

Create a shell script called fdread.sh:

#!/bin/bash
# Let us assign the file descriptor to file for input
# fd # 3 is Input file 
exec 3< /etc/resolv.conf
 
# Executes cat commands and  read input from 
# the file descriptor (fd) # 3 i.e. read input from /etc/resolv.conf file
cat <&3
 
# Close fd # 3
exec 3<&-

Save and close the file. Run it as follows:

chmod +x fdread.sh
./fdread.sh
← Assigns the file descriptor (fd) to file for outputHomeCloses the file descriptor (fd) →