Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Redirection (computing)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Piping== [[Image:Pipeline.svg|thumb|A pipeline of three programs run on a text terminal]] Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file. <syntaxhighlight lang="bash" inline>command1 | command2</syntaxhighlight> executes {{mono|command1}}, using its output as the input for {{mono|command2}} (commonly called [[Pipeline (Unix)|piping]], with the "<code>|</code>" character being known as the "pipe"). The two programs performing the commands may run in parallel with the only storage space being working buffers (Linux allows up to 64K for each buffer) plus whatever work space each command's processing requires. For example, a "sort" command is unable to produce any output until all input records have been read, as the very last record received just might turn out to be first in sorted order. Dr. Alexia Massalin's experimental operating system, [[Synthesis kernel#Massalin.27s Synthesis kernel|Synthesis]], would adjust the priority of each task as they ran according to the fullness of their input and output buffers.<ref>{{Cite web|url=https://lwn.net/Articles/270081/|title=KHB: Synthesis: An Efficient Implementation of Fundamental Operating Systems Services|website=lwn.net}}</ref> This produces the same end result as using two redirects and a temporary file, as in: <syntaxhighlight lang="console"> $ command1 > tempfile $ command2 < tempfile $ rm tempfile </syntaxhighlight> But here, {{mono|command2}} does not start executing until {{mono|command1}} has finished, and a sufficiently large scratch file is required to hold the intermediate results as well as whatever work space each task required. As an example, although DOS allows the "pipe" syntax, it employs this second approach. Thus, suppose some long-running program "Worker" produces various messages as it works, and that a second program, TimeStamp copies each record from ''stdin'' to ''stdout'', prefixed by the system's date and time when the record is received. A sequence such as <syntaxhighlight inline lang="bash">Worker | TimeStamp > LogFile.txt</syntaxhighlight> would produce timestamps only when Worker had finished, merely showing how swiftly its output file could be read and written. A good example for command piping is combining <code>[[echo (command)|echo]]</code> with another command to achieve something interactive in a non-interactive shell, e.g. <syntaxhighlight lang="bash" inline>echo -e 'user\npass' | ftp localhost</syntaxhighlight>. This runs the [[File Transfer Protocol|ftp]] client with input {{mono|user}}, press {{mono|return}}, then {{mono|pass}}. In casual use, the initial step of a pipeline is often <code>cat</code> or <code>echo</code>, reading from a file or string. This can often be replaced by input indirection or a [[here string]], and use of cat and piping rather than input redirection is known as [[useless use of cat]]. For example, the following commands: <syntaxhighlight lang="console"> $ cat infile | command $ echo $string | command $ echo -e 'user\npass' | ftp localhost </syntaxhighlight> can be replaced by: <syntaxhighlight lang="console"> $ command < infile $ command <<< $string $ ftp localhost <<< $'user\npass' </syntaxhighlight> As <code>echo</code> is often a shell-internal command, its use is not as criticized as cat, which is an external command.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)