Summarizing usage of the exec command

The following is a summary of the exec command for using various file handling-related operations:

Command

What it does

exec command

This command will replace shell and execute it. Therefore, it will not return to its original shell, which started it.

exec > data.txt

This opens data.txt for writing standard output.

exec < data.txt

This opens data.txt for reading standard input.

exec 3< data.txt

This opens data.txt for reading with descriptor 3.

sort <&3

This will sort the data.txt file.

exec 4> data.txt

This opens data.txt for writing with descriptor 4.

ll >&4

The output of ll is redirected to data.txt.

exec 6<&5

This makes fd 6 a copy of fd 5.

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.