Executing the command and storing the results in a file

The following is the syntax for storing the output of a command in a file:

    Command >& fd
    ./script >& fd
  

The following is the illustrative example script, file_08.sh:

#!/bin/bash 
exec 4> output.txt 
cat /proc/cpuinfo  >&4 
exec 3<&- 

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

    $ chmod u+x file_08.sh
    $ ./file_08.sh
  

Here's the output:

In this example, we have executed the command cat /proc/cpuinfo and we have stored the output in the file, output.txt.

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.