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 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.