Piping is a special form of redirection, using an additional instance of TCC for each instance of the piping specified in the command line.
You can create a pipe to send the standard output of a command (command1) to the standard input of another command (command2), and optionally also send the standard error as well:
what is sent to pipe
standard output only
command1 | command2
merge standard output and standard error
command1 |& command2
For example, to take the output of the ALIAS command (which displays a list of your aliases and their values) and pipe it to the external SORT utility to generate a sorted list, you would use the command:
alias | sort
To do the same thing and then pipe the sorted list to the internal LIST command for full-screen viewing:
alias | sort | list /s
TCC's output is normally in ANSI. If you want to redirect output in Unicode, you need to either use the /U startup option in TCC, or the Unicode Output option in TCMD.INI.
Like redirection, pipes are fully nestable. For example, you can invoke a batch file and send all of its output to another command with a pipe. A pipe on a command within the batch file will take effect for that command only; when the command is completed, output will revert to the pipe in use for the batch file as a whole. You may also have 2 or more pipes operating simultaneously if, for example, you have the pipes running in different windows or processes.
Processing each line received from a pipe
To process each line of text sent by the left side of a pipe in TCC, you may use the syntax below:
dir | for %file in (@CON:) command %file
This example shows how to pass each line of piped data to a command.
WARNINGS: TCC implements pipes by starting a new process for the receiving program. This process goes through the standard shell start-up procedure, including execution of the TCSTART file, for EACH receiving program. All of the sending and receiving programs run concurrently; the sending program writes to the pipe and the receiving program reads from the pipe. When the receiving program finds an End of File signal, it finishes reading and processing the piped data, and terminates. When you use pipes with TCC, make sure you consider the possible consequences from using a separate process to run the receiving program, especially that it cannot create/modify/delete environment variables of the sending program, and inclusion of a command to change directories in the TCSTART file may cause the new process to execute in a different directory. When you use more than one pipe in a single command, e.g. the second example above with LIST, each pipe adds another instance of TCC. If you need to execute the pipe in the same context, use in-process pipes (see below).
In-process pipes work like the old-style DOS pipes, by creating a temporary output file, redirecting STDOUT to that file, and then redirecting the temp file to STDIN of the following command. The syntax is:
command1 |! command2
This the same as doing:
command1 > temp.dat & command2 < temp.dat
but is easier to type & to read.
The advantage of in-process pipes is that command2 will be run in the same context as command1, so you can do things like modify environment variables without having them discarded when command2 exits. There are also some disadvantages to using this type of "pseudo-pipe" -- it will usually be slower than a true pipe; it will use some disk space for its temp file; and command2 will not be started until command1 has exited.