Linux+: Linux Shell 14 – Shell Command Redirection - Advanced


Jarret W. Buse

Linux+: Linux Shell 14 – Shell Command Redirection - Advanced

Basic redirection can come in handy, but there are many more possibilities. There also exists other redirections which may not be necessary for the Linux+ Certification, but definitely important to know. These are more advanced topics on the previous redirection commands from the Linux+: Linux Shell 13 – Shell Command Redirection article.

NOTE: These redirection techniques are for the BASH shell.

To create an empty file or to remove all the contents of a file to empty it, you can use one of the following commands:

: > filename
> filename

NOTE: The second command does not work on all shells, but the first one should work.

Each of the input and output devices have a default file descriptor as follows:

  • stdin – FD 0
  • stdout – FD 1
  • stderr – FD 2

So, to make this work using the FD numbers, the number and redirection should not have a space in between them. For example, to redirect the standard output of the 'ls' command to a file called 'out', you would do the following:

ls 1>out

To also cause the standard error to be output to a file called 'error', we could perform the following command:

ls 1>out 2>error

Since the command worked, the output is in the file 'out' and the file 'error' is empty. If we should do something that causes an error, then the results be the opposite. Let's try the next command:

las 1>out 2>error

If no command exists called 'las', then the file 'out' is empty and the file 'error' should contain the error messages.
If you were running an application which has been giving you problems, you can execute the command as follows:

command 2>error

When done with the application, all error messages are in the file 'error', and you can view them as needed.
NOTE: To append the messages to the file, use the >> instead of the > redirection symbol.

What if you wanted the output file to contain both the standard out and standard error? To represent both FD 1 and FD 2 you use the ampersand '&' as shown:

ls /ther/ ~ &>out

In this command, the files are being listed for the root folder called 'ther' (which should not exist) and the home folder. The first part should generate an error and the second part should create a file listing. Both messages should be output to the 'out' file. If the 'out' file did exist, then all content should be erased and the new messages placed into the file. If you want to append the information to what already exists, then the command would be:

ls /ther/ ~ &>>out

If we wish to send the standard error to standard out, we can use the following command:

command 1>out 2>&1

Here, we see that the standard error (2) is sent to standard out (1). The ampersand (&) is used to specify that the output is sent to something other than a filename. If not used, the standard error would be sent to a file named '1'. Standard output is then sent to a file named 'out'.

You may wonder what is the difference using the File Descriptors than the previous article examples? In the previous article, I noted that the output redirector had to be used last. When using the File Descriptors, the order is not important except in the previous example.

When FD 2 is redirected to FD 1, I must set up FD 1 first before reassigning one to another. In this case, I had to specify where FD 1 went before I reassigned FD 2 to FD 1. If I switched the two, then the error messages would not go to the 'out' file.

Now, for scripting purposes, I can assign a file to a File Descriptor. The file can be used for input and output. If I want to assign the File Descriptor '4' to the file 'out1' I would use the following command:

exec 4<>out1

Now, with scripting or in a terminal, I can use the File Descriptor over and over until the terminal is closed or I close the File Descriptor as follows:

exec 4>&-
The values for the FD can be any number up and including 1023. It is best not to mess with the File Descriptors 0, 1 and 2. You can do it, but this may cause issues.

NOTE: The file is for input and output. If you use it for output and then perform another output command, it is appended to and not overwritten. Even using the command '>&#' or '>#', the file is not overwritten. The File Descriptor must be closed and the file emptied and then reassigned.

As I noted, the file can be used for input. In the next example, I will assign the File Descriptor 10 to the file 'out'. I will then fill it with a listing of the home folder and then use that output as an input to check for a file starting with the letter 'l'.

exec 10<>out
ls >&10
exec 10>&-
exec 10<>out
grep l* <&10
exec 10>&-

The reason to close the file and reopen it is to place the file pointer back at the beginning of the file. You can move ahead in the file by using the 'read' command and specify the number of characters to move ahead in the file. For instance, to move ahead 15 characters, you would issue the following command:

read -n 15 >&10

If you want to place information into the file, you can use the 'echo' command as shown:

echo -n >&10

Try the File Descriptors to understand them better. You can also open multiple File Descriptors at once.



Jarret W. Buse

could you show me more example using & in script?
I am working on another article to cover just examples for using & in a redirection. I will let you know when it it is put on the site. Sorry it took me a while to get back on this topic. Thanks.
$100 Digital Ocean Credit
Get a free VM to test out Linux!