<?xml version="1.0" encoding="UTF-8"?>
<!-- **************************************************************************
.... For copyright and licensing terms, see the file named COPYING.
.... **************************************************************************
.-->
<?xml-stylesheet href="docbook-xml.css" type="text/css"?>

<refentry id="pipe">

<refmeta xmlns:xi="http://www.w3.org/2001/XInclude">
<refentrytitle>pipe</refentrytitle>
<manvolnum>1</manvolnum>
<refmiscinfo class="manual">user commands</refmiscinfo>
<refmiscinfo class="source">nosh</refmiscinfo>
<xi:include href="version.xml" />
</refmeta>

<refnamediv><refname>pipe</refname><refpurpose>execute two commands with a pipe between them</refpurpose></refnamediv>

<refsynopsisdiv>
<cmdsynopsis>
<command>pipe</command>
<arg choice='opt'>--inwards</arg>
<arg choice='opt'>--error</arg>
<arg choice='opt'>--grandchild</arg>
<arg choice='opt'>--ignore-children</arg>
<arg choice='opt'>--separator <replaceable>alt-sep</replaceable></arg>
<arg choice='req'><replaceable>left-prog</replaceable></arg>
<arg choice='req'>|</arg>
<arg choice='req'><replaceable>right-prog</replaceable></arg>
</cmdsynopsis>
</refsynopsisdiv>

<refsection><title>Description</title>

<para>
<command>pipe</command> is a chain-loading utility that opens a pipe with the <citerefentry><refentrytitle>pipe</refentrytitle><manvolnum>2</manvolnum></citerefentry> library function, and runs <replaceable>left-prog</replaceable> with its standard output writing to the pipe and <replaceable>right-prog</replaceable> with its standard input reading from the pipe.
It chain loads one of the twain and runs the other as a child process created by <citerefentry><refentrytitle>fork</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
Both are executed with the <citerefentry><refentrytitle>execvp</refentrytitle><manvolnum>3</manvolnum></citerefentry> function.
</para>

<para>
<replaceable>left-prog</replaceable> and <replaceable>right-prog</replaceable> may contain their own command line options, which <command>pipe</command> will ignore.
</para>

<para>
Normally, in outwards mode, <replaceable>left-prog</replaceable> is chain loaded in the original process and <replaceable>right-prog</replaceable> is forked and run in the new child process.
If the <arg choice='opt'>--inwards</arg> command line option is used, <replaceable>right-prog</replaceable> is chain loaded in the original process and <replaceable>left-prog</replaceable> is forked and run in the new child process.
</para>

<para>
The <arg choice='opt'>--separator</arg> command line option specifies an alternative separator argument, to scan for in place of <arg choice='req'>|</arg>.
</para>

<para>
The <arg choice='opt'>--grandchild</arg> command line option causes whichever side of the pipe that is run in the forked child to perform a second fork and then exit, so that that side of the pipe runs in an orphaned child process.
This is useful when it is known that the side of the pipe that runs in the forked parent never cleans up its terminated children.
However, it makes the process tree less obvious.
An alternative to this is the <arg choice='opt'>--ignore-children</arg> command line option which makes the parent program ignore the <code>SIGCHLD</code> signal.
</para>

</refsection>
<refsection><title>Usage</title>

<para>
The primary use of <command>pipe</command> is from a <citerefentry><refentrytitle>nosh</refentrytitle><manvolnum>1</manvolnum></citerefentry> script to enact a pipeline.
When invoking <command>pipe</command> from within a shell script, remember that <arg choice='req'>|</arg> must be escaped or quoted in order to prevent the shell from recognizing it.
</para>

<para>
<command>pipe</command> looks for the leftmost instance of <arg choice='req'>|</arg>, and separates left from right there.
Further instances of <arg choice='req'>|</arg> in <replaceable>right-prog</replaceable> are left alone.
This allows longer pipelines, by using another <command>pipe</command> in <replaceable>right-prog</replaceable>.
For example:
<userinput>pipe cat head body foot | pipe sort | uniq</userinput>
</para>

<para>
Piping both standard error and standard output is done as it would be in a shell: by an explicit redirection in <replaceable>left-prog</replaceable>, using <citerefentry><refentrytitle>fdmove</refentrytitle><manvolnum>1</manvolnum></citerefentry> or a similar utility.
(In the shell, the explicit redirection would be <userinput>2>&amp;1</userinput>.)
For example: <userinput>pipe fdmove -c 2 1 wibble | less</userinput>
Remember that the redirection must be enacted within <replaceable>left-prog</replaceable>.
<userinput>fdmove -c 2 1 pipe wibble | less</userinput> would redirect the standard error of the entire pipeline.
</para>

</refsection>
<refsection><title>Author</title>
<para><author><personname><firstname>Jonathan</firstname> <surname>de Boyne Pollard</surname></personname></author></para>
</refsection>

</refentry>
