InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Using Multiple Redirects on One Line in bash
Bash

Using Multiple Redirects on One Line in bash

InfinityCoder January 12, 2017

You want to redirect output to several different places.

Use redirection with file numbers to open all the files that you want to use. For example:

1
2
$ divert 3> file.three 4> file.four 5> file.five 6> else.where
$

where divert might be a shell script with various commands whose output you want to send to different places.

For example, you might write divert to contain lines like this: echo option $OPTSTR >&5.

That is, our divert shell script could direct its output to various different descriptors which the invoking program can send to different destinations.
Similarly, if divert was a C program executable, you could actually write to descriptors 3, 4, 5, and 6 without any need for open( ) calls.

In an earlier recipe we explained that each file descriptor is indicated by a number, starting at 0 (zero). So standard input is 0, out is 1, and error is 2.

That means that you could redirect standard output with the slightly more verbose 1> (rather than a simple >) followed by a filename, but there’s no need.

The shorthand > is fine. It also means that you can have the shell open up any number of arbitrary file descriptors
and have them set to write various files so that the program that the shell then invokes from the command line can use these opened file descriptors without further ado.
While we don’t recommend this technique, it is intriguing.

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Showing All Hidden (dot) Files in the Current Directory in bash
You want to see only hidden (dot) files in a …

Showing All Hidden (dot) Files in the Current Directory in bash

Supplying a Default Date in bash
You want your script to provide a useful default date, …

Supplying a Default Date in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2018 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap