Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm studying for my LPIC-1 and I'm confused by the <> redirection operator. I know the official definition for this is that it "causes the specified file to be used for both standard input and standard output"... but I'm still confused.

Could someone please provide an example of how/when I would use the <> redirection operator in Linux?

share|improve this question
    
A good but German-only explanation on redirection in bash which I know about can be found here. –  nuchoco Oct 5 '10 at 9:52

2 Answers 2

up vote 3 down vote accepted

An example would be when you need to supply a program input data, and also send output data somewhere (eg. a file):

$ sort < data.txt > result.txt

this runs the data in data.txt through the sort command, and sends the output to result.txt. It is identical to doing this:

$ sort data.txt > result.txt

Think of it like this:

(2 <- 1) -> 3

The middle file is fed to the program at number 2, and the result is finally sent to 3.

share|improve this answer
    
Thanks John. Much appreciated! –  Mike B Nov 3 '09 at 3:32
    
You're most welcome! :) –  John T Nov 3 '09 at 4:10
    
I think there is a functional difference between "sort data.txt" and "sort < data.txt". In the former, the utility needs to understand how to open a file in the filesystem and read it. In the later, the utility only needs to understand how to read text from the "standard in", which for an interactive terminal access is commonly the keyboard. I may be wrong. –  pcapademic Nov 3 '09 at 10:35

If John T's answer was what you wanted, then you mis-stated the question :)

He's succinctly described the < and > operators, but you asked about <>. This is a rarely used operator which I'm not sure I'd expect to see in LPIC-1, but just for the record (given that you've probably done with that exam by now):

When you run interactively a command without redirection its standard input, output and error will normally be attached to the tty; it will read from stdin and write to stdout/stderr. Though you might not expect it, those file descriptors are typically opened read-write. Try running a script like

#!/bin/bash
echo "I'm writing to stdin!" >&0

Running this bare will work, at least in current bash versions. However, when you use the < and > redirectors, the stdin and stdout file descriptors are only opened for read and write respectively, so if you invoke this with input redirected from a file -

./write-to-stdin < /tmp/some-input

it won't work (reasonably enough). If you really want a program you invoke with redirection to be able to write and write to a single file descriptor, <> will do the job.

./read-and-write-stdin   <> /tmp/some-input-and-output
./read-and-write-stdout 1<> /tmp/some-input-and-output

As most programs only expect stdin to be a read descriptor and stdout to be a write descriptor, you can probably see why this is rather rarely useful, but if you're writing subordinate scripts (or using functions) you can take advantage of the behaviour yourself.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.