Only command after pipe executes, how does the pipe work in these scenarios? The 2019 Stack Overflow Developer Survey Results Are Inbash_history: comment out dangerous commands: `#`syslog-ng won't write to log file - “No such file or directory”How to avoid broken pipe in commands with cat?How to tell the 'bottleneck' in a muti-pipe commandPipe inside exec command won't workWhy isn't `|` treated literally in a glob pattern?When typing ctrl-c in a terminal, why isn't the foreground job terminated until it completes?Under what conditions exactly does SIGPIPE happen?How to write a function that reliably exits (with a specified status) the current process?How to quit command prompt after a sleeping command executes?
For what reasons would an animal species NOT cross a *horizontal* land bridge?
Can there be female White Walkers?
Old scifi movie from the 50s or 60s with men in solid red uniforms who interrogate a spy from the past
How to display lines in a file like ls displays files in a directory?
Why can't devices on different VLANs, but on the same subnet, communicate?
"as much details as you can remember"
How to type a long/em dash `—`
Straighten subgroup lattice
Can you cast a spell on someone in the Ethereal Plane, if you are on the Material Plane and have the True Seeing spell active?
What is this sharp, curved notch on my knife for?
A word that means fill it to the required quantity
How to obtain a position of last non-zero element
Finding the area between two curves with Integrate
Are spiders unable to hurt humans, especially very small spiders?
What is preventing me from simply constructing a hash that's lower than the current target?
Kerning for subscripts of sigma?
Accepted by European university, rejected by all American ones I applied to? Possible reasons?
Can an undergraduate be advised by a professor who is very far away?
How to charge AirPods to keep battery healthy?
Unitary representations of finite groups over finite fields
If my opponent casts Ultimate Price on my Phantasmal Bear, can I save it by casting Snap or Curfew?
Worn-tile Scrabble
What do I do when my TA workload is more than expected?
What is the meaning of Triage in Cybersec world?
Only command after pipe executes, how does the pipe work in these scenarios?
The 2019 Stack Overflow Developer Survey Results Are Inbash_history: comment out dangerous commands: `#`syslog-ng won't write to log file - “No such file or directory”How to avoid broken pipe in commands with cat?How to tell the 'bottleneck' in a muti-pipe commandPipe inside exec command won't workWhy isn't `|` treated literally in a glob pattern?When typing ctrl-c in a terminal, why isn't the foreground job terminated until it completes?Under what conditions exactly does SIGPIPE happen?How to write a function that reliably exits (with a specified status) the current process?How to quit command prompt after a sleeping command executes?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
What happens in the following two examples? Why is only the second command in each pipeline executed? How does the pipe work here?
example 1
$ cat /var/log/messages | date
Mon Apr 8 17:10:02 IST 2019
example 2
$ cat /var/log/messages | w
17:10:34 up 8 days, 1:24, 6 users, load average: 0.63, 0.49, 0.61
bash pipe
add a comment |
What happens in the following two examples? Why is only the second command in each pipeline executed? How does the pipe work here?
example 1
$ cat /var/log/messages | date
Mon Apr 8 17:10:02 IST 2019
example 2
$ cat /var/log/messages | w
17:10:34 up 8 days, 1:24, 6 users, load average: 0.63, 0.49, 0.61
bash pipe
add a comment |
What happens in the following two examples? Why is only the second command in each pipeline executed? How does the pipe work here?
example 1
$ cat /var/log/messages | date
Mon Apr 8 17:10:02 IST 2019
example 2
$ cat /var/log/messages | w
17:10:34 up 8 days, 1:24, 6 users, load average: 0.63, 0.49, 0.61
bash pipe
What happens in the following two examples? Why is only the second command in each pipeline executed? How does the pipe work here?
example 1
$ cat /var/log/messages | date
Mon Apr 8 17:10:02 IST 2019
example 2
$ cat /var/log/messages | w
17:10:34 up 8 days, 1:24, 6 users, load average: 0.63, 0.49, 0.61
bash pipe
bash pipe
edited Apr 8 at 12:23
Kusalananda♦
141k17263439
141k17263439
asked Apr 8 at 11:45
supermansuperman
145
145
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Both commands in both examples are executed. Only the output of the command after the pipe symbol is actually shown.
In your pipeline, the contents of the /var/log/messages
file is piped into the input of the date
command by running cat
on the left hand side of the pipeline.
The date
command does not care about its standard input stream and will ignore any data arriving there. It will, however, produce its own output, which it does on its standard output stream.
The net visible result is that the output of the cat
is ignored and discarded, while the output of date
is show in the terminal (or wherever the output of the pipeline goes).
What actually happens with the output of the cat
command is that, since date
won't read it, the cat
command is temporarily blocked, waiting for its output to be read (after successfully outputting however much the pipe's buffer can hold; it does not know date
will never read it). The date
command does its thing and outputs its string, after which it terminates.
At that point, when date
terminates and the standard input stream of date
is closed, the cat
command receives a PIPE
signal by the shell. This signal tells cat
that whatever data it's trying to write to its standard output is never going to be read, and it too terminates (terminating on PIPE
is the default action for this signal). The rest of the file that cat
was reading from is never read, and the data in the pipe's buffer is discarded when the shell releases the memory associated with it.
The exact same thing happens with date
replaced by w
, or any other command that does not read its standard input stream.
You can compare this with a using a command that actually does read its standard input stream:
cat /var/log/messages | tr '[:lower:]' '[:upper:]'
or, without the pipe (as the cat
is actually not needed in any of these examples),
tr '[:lower:]' '[:upper:]' </var/log/messages
add a comment |
That's how pipes work: The left hand side command's output is send as input into the right hand side command.
If you want to run two commands one after another, use a semicolon:
cat /var/log/messages ; date
If you want to run the second command only if the first one is successful, use &&
:
cat /var/log/messages && date
If you want to run the second command only if the first one fails, use ||
:
cat /var/log/messages || date
If you want to run the first command in the background and run the second one at the same time, use &
cat /var/log/messages & date
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511215%2fonly-command-after-pipe-executes-how-does-the-pipe-work-in-these-scenarios%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Both commands in both examples are executed. Only the output of the command after the pipe symbol is actually shown.
In your pipeline, the contents of the /var/log/messages
file is piped into the input of the date
command by running cat
on the left hand side of the pipeline.
The date
command does not care about its standard input stream and will ignore any data arriving there. It will, however, produce its own output, which it does on its standard output stream.
The net visible result is that the output of the cat
is ignored and discarded, while the output of date
is show in the terminal (or wherever the output of the pipeline goes).
What actually happens with the output of the cat
command is that, since date
won't read it, the cat
command is temporarily blocked, waiting for its output to be read (after successfully outputting however much the pipe's buffer can hold; it does not know date
will never read it). The date
command does its thing and outputs its string, after which it terminates.
At that point, when date
terminates and the standard input stream of date
is closed, the cat
command receives a PIPE
signal by the shell. This signal tells cat
that whatever data it's trying to write to its standard output is never going to be read, and it too terminates (terminating on PIPE
is the default action for this signal). The rest of the file that cat
was reading from is never read, and the data in the pipe's buffer is discarded when the shell releases the memory associated with it.
The exact same thing happens with date
replaced by w
, or any other command that does not read its standard input stream.
You can compare this with a using a command that actually does read its standard input stream:
cat /var/log/messages | tr '[:lower:]' '[:upper:]'
or, without the pipe (as the cat
is actually not needed in any of these examples),
tr '[:lower:]' '[:upper:]' </var/log/messages
add a comment |
Both commands in both examples are executed. Only the output of the command after the pipe symbol is actually shown.
In your pipeline, the contents of the /var/log/messages
file is piped into the input of the date
command by running cat
on the left hand side of the pipeline.
The date
command does not care about its standard input stream and will ignore any data arriving there. It will, however, produce its own output, which it does on its standard output stream.
The net visible result is that the output of the cat
is ignored and discarded, while the output of date
is show in the terminal (or wherever the output of the pipeline goes).
What actually happens with the output of the cat
command is that, since date
won't read it, the cat
command is temporarily blocked, waiting for its output to be read (after successfully outputting however much the pipe's buffer can hold; it does not know date
will never read it). The date
command does its thing and outputs its string, after which it terminates.
At that point, when date
terminates and the standard input stream of date
is closed, the cat
command receives a PIPE
signal by the shell. This signal tells cat
that whatever data it's trying to write to its standard output is never going to be read, and it too terminates (terminating on PIPE
is the default action for this signal). The rest of the file that cat
was reading from is never read, and the data in the pipe's buffer is discarded when the shell releases the memory associated with it.
The exact same thing happens with date
replaced by w
, or any other command that does not read its standard input stream.
You can compare this with a using a command that actually does read its standard input stream:
cat /var/log/messages | tr '[:lower:]' '[:upper:]'
or, without the pipe (as the cat
is actually not needed in any of these examples),
tr '[:lower:]' '[:upper:]' </var/log/messages
add a comment |
Both commands in both examples are executed. Only the output of the command after the pipe symbol is actually shown.
In your pipeline, the contents of the /var/log/messages
file is piped into the input of the date
command by running cat
on the left hand side of the pipeline.
The date
command does not care about its standard input stream and will ignore any data arriving there. It will, however, produce its own output, which it does on its standard output stream.
The net visible result is that the output of the cat
is ignored and discarded, while the output of date
is show in the terminal (or wherever the output of the pipeline goes).
What actually happens with the output of the cat
command is that, since date
won't read it, the cat
command is temporarily blocked, waiting for its output to be read (after successfully outputting however much the pipe's buffer can hold; it does not know date
will never read it). The date
command does its thing and outputs its string, after which it terminates.
At that point, when date
terminates and the standard input stream of date
is closed, the cat
command receives a PIPE
signal by the shell. This signal tells cat
that whatever data it's trying to write to its standard output is never going to be read, and it too terminates (terminating on PIPE
is the default action for this signal). The rest of the file that cat
was reading from is never read, and the data in the pipe's buffer is discarded when the shell releases the memory associated with it.
The exact same thing happens with date
replaced by w
, or any other command that does not read its standard input stream.
You can compare this with a using a command that actually does read its standard input stream:
cat /var/log/messages | tr '[:lower:]' '[:upper:]'
or, without the pipe (as the cat
is actually not needed in any of these examples),
tr '[:lower:]' '[:upper:]' </var/log/messages
Both commands in both examples are executed. Only the output of the command after the pipe symbol is actually shown.
In your pipeline, the contents of the /var/log/messages
file is piped into the input of the date
command by running cat
on the left hand side of the pipeline.
The date
command does not care about its standard input stream and will ignore any data arriving there. It will, however, produce its own output, which it does on its standard output stream.
The net visible result is that the output of the cat
is ignored and discarded, while the output of date
is show in the terminal (or wherever the output of the pipeline goes).
What actually happens with the output of the cat
command is that, since date
won't read it, the cat
command is temporarily blocked, waiting for its output to be read (after successfully outputting however much the pipe's buffer can hold; it does not know date
will never read it). The date
command does its thing and outputs its string, after which it terminates.
At that point, when date
terminates and the standard input stream of date
is closed, the cat
command receives a PIPE
signal by the shell. This signal tells cat
that whatever data it's trying to write to its standard output is never going to be read, and it too terminates (terminating on PIPE
is the default action for this signal). The rest of the file that cat
was reading from is never read, and the data in the pipe's buffer is discarded when the shell releases the memory associated with it.
The exact same thing happens with date
replaced by w
, or any other command that does not read its standard input stream.
You can compare this with a using a command that actually does read its standard input stream:
cat /var/log/messages | tr '[:lower:]' '[:upper:]'
or, without the pipe (as the cat
is actually not needed in any of these examples),
tr '[:lower:]' '[:upper:]' </var/log/messages
edited Apr 8 at 12:24
answered Apr 8 at 12:05
Kusalananda♦Kusalananda
141k17263439
141k17263439
add a comment |
add a comment |
That's how pipes work: The left hand side command's output is send as input into the right hand side command.
If you want to run two commands one after another, use a semicolon:
cat /var/log/messages ; date
If you want to run the second command only if the first one is successful, use &&
:
cat /var/log/messages && date
If you want to run the second command only if the first one fails, use ||
:
cat /var/log/messages || date
If you want to run the first command in the background and run the second one at the same time, use &
cat /var/log/messages & date
add a comment |
That's how pipes work: The left hand side command's output is send as input into the right hand side command.
If you want to run two commands one after another, use a semicolon:
cat /var/log/messages ; date
If you want to run the second command only if the first one is successful, use &&
:
cat /var/log/messages && date
If you want to run the second command only if the first one fails, use ||
:
cat /var/log/messages || date
If you want to run the first command in the background and run the second one at the same time, use &
cat /var/log/messages & date
add a comment |
That's how pipes work: The left hand side command's output is send as input into the right hand side command.
If you want to run two commands one after another, use a semicolon:
cat /var/log/messages ; date
If you want to run the second command only if the first one is successful, use &&
:
cat /var/log/messages && date
If you want to run the second command only if the first one fails, use ||
:
cat /var/log/messages || date
If you want to run the first command in the background and run the second one at the same time, use &
cat /var/log/messages & date
That's how pipes work: The left hand side command's output is send as input into the right hand side command.
If you want to run two commands one after another, use a semicolon:
cat /var/log/messages ; date
If you want to run the second command only if the first one is successful, use &&
:
cat /var/log/messages && date
If you want to run the second command only if the first one fails, use ||
:
cat /var/log/messages || date
If you want to run the first command in the background and run the second one at the same time, use &
cat /var/log/messages & date
answered Apr 8 at 11:52
chorobachoroba
27.1k45176
27.1k45176
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511215%2fonly-command-after-pipe-executes-how-does-the-pipe-work-in-these-scenarios%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown