Bash: child process and pipelining2019 Community Moderator ElectionWhy is my variable local in one 'while read' loop, but not in another seemingly similar loop?How to copy or read the return value (exit status) from AWK script into the shell script (bash) to be used in if statement to compareModify $READLINE_LINE and $READLINE_POINT values inside bash scriptWhy there is such a difference in execution time of echo and cat?Why does bash clear OLDPWD when a child script is started?Infinite while loop issue using readsed /PATTERN/SUB/ $VAR?null string as command in while loop: empty/null-commandHow to make reading and writing the same file in the same pipeline always “fail”?Why does `source foo && true` exit the script in bash?Parent process blocking trying to read output from zombie child process
Pronouncing Homer as in modern Greek
Superhero words!
Meta programming: Declare a new struct on the fly
Could solar power be utilized and substitute coal in the 19th century?
Can I Retrieve Email Addresses from BCC?
Hostile work environment after whistle-blowing on coworker and our boss. What do I do?
Why are all the doors on Ferenginar (the Ferengi home world) far shorter than the average Ferengi?
Latex for-and in equation
Would it be legal for a US State to ban exports of a natural resource?
Giant Toughroad SLR 2 for 200 miles in two days, will it make it?
Can I rely on these GitHub repository files?
Is there a problem with hiding "forgot password" until it's needed?
Can a Gentile theist be saved?
Why is delta-v is the most useful quantity for planning space travel?
In Star Trek IV, why did the Bounty go back to a time when whales were already rare?
Proof of Lemma: Every integer can be written as a product of primes
Blender - show edges angles “direction”
Are taller landing gear bad for aircraft, particulary large airliners?
How do I repair my stair bannister?
Is exact Kanji stroke length important?
Can somebody explain Brexit in a few child-proof sentences?
Teaching indefinite integrals that require special-casing
Perfect riffle shuffles
What do you call the infoboxes with text and sometimes images on the side of a page we find in textbooks?
Bash: child process and pipelining
2019 Community Moderator ElectionWhy is my variable local in one 'while read' loop, but not in another seemingly similar loop?How to copy or read the return value (exit status) from AWK script into the shell script (bash) to be used in if statement to compareModify $READLINE_LINE and $READLINE_POINT values inside bash scriptWhy there is such a difference in execution time of echo and cat?Why does bash clear OLDPWD when a child script is started?Infinite while loop issue using readsed /PATTERN/SUB/ $VAR?null string as command in while loop: empty/null-commandHow to make reading and writing the same file in the same pipeline always “fail”?Why does `source foo && true` exit the script in bash?Parent process blocking trying to read output from zombie child process
The purpose of my script is to count how many lines there are in a file
. I know I could use wc
for example but the purpose of this exercise is to understand processes and pipelines in Linux.
The script executed on my terminal:
C=0; cat file | while read line ; do C=$[ $C + 1 ] ; done ; echo $C
I always get 0
or whatever number I inizialize C
variable.
In my textbook they explain this behavior saying that for every pipeline a new child process is created, it inherits all father variables but when the child dies the father still "see" his old values. And I'm ok with that.
What I don't understand is that I only see one pipeline, between C=0; cat file
and while read line ; do C=$[ $C + 1 ] ; done ; echo $C
. So I'm guessing that the second part is executed by the child (echo
too) so why it prints the wrong value? Shouldn't the child increase C
variable AND print the correct value as it belongs to the same pipe?
bash pipe echo
add a comment |
The purpose of my script is to count how many lines there are in a file
. I know I could use wc
for example but the purpose of this exercise is to understand processes and pipelines in Linux.
The script executed on my terminal:
C=0; cat file | while read line ; do C=$[ $C + 1 ] ; done ; echo $C
I always get 0
or whatever number I inizialize C
variable.
In my textbook they explain this behavior saying that for every pipeline a new child process is created, it inherits all father variables but when the child dies the father still "see" his old values. And I'm ok with that.
What I don't understand is that I only see one pipeline, between C=0; cat file
and while read line ; do C=$[ $C + 1 ] ; done ; echo $C
. So I'm guessing that the second part is executed by the child (echo
too) so why it prints the wrong value? Shouldn't the child increase C
variable AND print the correct value as it belongs to the same pipe?
bash pipe echo
As a side note, shell variables should be lower-case. Environment variables should be upper-case.
– ctrl-alt-delor
yesterday
add a comment |
The purpose of my script is to count how many lines there are in a file
. I know I could use wc
for example but the purpose of this exercise is to understand processes and pipelines in Linux.
The script executed on my terminal:
C=0; cat file | while read line ; do C=$[ $C + 1 ] ; done ; echo $C
I always get 0
or whatever number I inizialize C
variable.
In my textbook they explain this behavior saying that for every pipeline a new child process is created, it inherits all father variables but when the child dies the father still "see" his old values. And I'm ok with that.
What I don't understand is that I only see one pipeline, between C=0; cat file
and while read line ; do C=$[ $C + 1 ] ; done ; echo $C
. So I'm guessing that the second part is executed by the child (echo
too) so why it prints the wrong value? Shouldn't the child increase C
variable AND print the correct value as it belongs to the same pipe?
bash pipe echo
The purpose of my script is to count how many lines there are in a file
. I know I could use wc
for example but the purpose of this exercise is to understand processes and pipelines in Linux.
The script executed on my terminal:
C=0; cat file | while read line ; do C=$[ $C + 1 ] ; done ; echo $C
I always get 0
or whatever number I inizialize C
variable.
In my textbook they explain this behavior saying that for every pipeline a new child process is created, it inherits all father variables but when the child dies the father still "see" his old values. And I'm ok with that.
What I don't understand is that I only see one pipeline, between C=0; cat file
and while read line ; do C=$[ $C + 1 ] ; done ; echo $C
. So I'm guessing that the second part is executed by the child (echo
too) so why it prints the wrong value? Shouldn't the child increase C
variable AND print the correct value as it belongs to the same pipe?
bash pipe echo
bash pipe echo
asked yesterday
xdolaxdola
1789
1789
As a side note, shell variables should be lower-case. Environment variables should be upper-case.
– ctrl-alt-delor
yesterday
add a comment |
As a side note, shell variables should be lower-case. Environment variables should be upper-case.
– ctrl-alt-delor
yesterday
As a side note, shell variables should be lower-case. Environment variables should be upper-case.
– ctrl-alt-delor
yesterday
As a side note, shell variables should be lower-case. Environment variables should be upper-case.
– ctrl-alt-delor
yesterday
add a comment |
1 Answer
1
active
oldest
votes
What I don't understand is that I only see one pipeline, between
C=0; cat file
andwhile read line ; do C=$[ $C + 1 ] ; done ; echo $C
.
No, that's not how it parses. You actually have three "pipelines":
C=0
cat file | while read line ; do C=$[ $C + 1 ] ; done
echo $C
(Granted, the first and the third are degenerate single-command pipelines, but technically they still are just that. A pipeline is a sequence of one or more commands separated by the control operator '|')
In other words, the first and last semicolons separate pipelines. The ones between while
and done
don't, since they're a part of the while
compound command.
A simpler example without any compound commands:
$ echo hello | tr a-z x ; echo you | tr a-z y
xxxxx
yyy
Here, we can easily see that only hello
goes to the first tr
, and only you
goes to the second tr
, i.e. the semicolon separates the pipelines.
The question Why is my variable local in one 'while read' loop, but not in another seemingly similar loop? contains a number of ways of doing what you tried to do, to be able to output the value of C
as updated by the loop.
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%2f508338%2fbash-child-process-and-pipelining%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
What I don't understand is that I only see one pipeline, between
C=0; cat file
andwhile read line ; do C=$[ $C + 1 ] ; done ; echo $C
.
No, that's not how it parses. You actually have three "pipelines":
C=0
cat file | while read line ; do C=$[ $C + 1 ] ; done
echo $C
(Granted, the first and the third are degenerate single-command pipelines, but technically they still are just that. A pipeline is a sequence of one or more commands separated by the control operator '|')
In other words, the first and last semicolons separate pipelines. The ones between while
and done
don't, since they're a part of the while
compound command.
A simpler example without any compound commands:
$ echo hello | tr a-z x ; echo you | tr a-z y
xxxxx
yyy
Here, we can easily see that only hello
goes to the first tr
, and only you
goes to the second tr
, i.e. the semicolon separates the pipelines.
The question Why is my variable local in one 'while read' loop, but not in another seemingly similar loop? contains a number of ways of doing what you tried to do, to be able to output the value of C
as updated by the loop.
add a comment |
What I don't understand is that I only see one pipeline, between
C=0; cat file
andwhile read line ; do C=$[ $C + 1 ] ; done ; echo $C
.
No, that's not how it parses. You actually have three "pipelines":
C=0
cat file | while read line ; do C=$[ $C + 1 ] ; done
echo $C
(Granted, the first and the third are degenerate single-command pipelines, but technically they still are just that. A pipeline is a sequence of one or more commands separated by the control operator '|')
In other words, the first and last semicolons separate pipelines. The ones between while
and done
don't, since they're a part of the while
compound command.
A simpler example without any compound commands:
$ echo hello | tr a-z x ; echo you | tr a-z y
xxxxx
yyy
Here, we can easily see that only hello
goes to the first tr
, and only you
goes to the second tr
, i.e. the semicolon separates the pipelines.
The question Why is my variable local in one 'while read' loop, but not in another seemingly similar loop? contains a number of ways of doing what you tried to do, to be able to output the value of C
as updated by the loop.
add a comment |
What I don't understand is that I only see one pipeline, between
C=0; cat file
andwhile read line ; do C=$[ $C + 1 ] ; done ; echo $C
.
No, that's not how it parses. You actually have three "pipelines":
C=0
cat file | while read line ; do C=$[ $C + 1 ] ; done
echo $C
(Granted, the first and the third are degenerate single-command pipelines, but technically they still are just that. A pipeline is a sequence of one or more commands separated by the control operator '|')
In other words, the first and last semicolons separate pipelines. The ones between while
and done
don't, since they're a part of the while
compound command.
A simpler example without any compound commands:
$ echo hello | tr a-z x ; echo you | tr a-z y
xxxxx
yyy
Here, we can easily see that only hello
goes to the first tr
, and only you
goes to the second tr
, i.e. the semicolon separates the pipelines.
The question Why is my variable local in one 'while read' loop, but not in another seemingly similar loop? contains a number of ways of doing what you tried to do, to be able to output the value of C
as updated by the loop.
What I don't understand is that I only see one pipeline, between
C=0; cat file
andwhile read line ; do C=$[ $C + 1 ] ; done ; echo $C
.
No, that's not how it parses. You actually have three "pipelines":
C=0
cat file | while read line ; do C=$[ $C + 1 ] ; done
echo $C
(Granted, the first and the third are degenerate single-command pipelines, but technically they still are just that. A pipeline is a sequence of one or more commands separated by the control operator '|')
In other words, the first and last semicolons separate pipelines. The ones between while
and done
don't, since they're a part of the while
compound command.
A simpler example without any compound commands:
$ echo hello | tr a-z x ; echo you | tr a-z y
xxxxx
yyy
Here, we can easily see that only hello
goes to the first tr
, and only you
goes to the second tr
, i.e. the semicolon separates the pipelines.
The question Why is my variable local in one 'while read' loop, but not in another seemingly similar loop? contains a number of ways of doing what you tried to do, to be able to output the value of C
as updated by the loop.
edited yesterday
answered yesterday
ilkkachuilkkachu
62.5k10103179
62.5k10103179
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%2f508338%2fbash-child-process-and-pipelining%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
As a side note, shell variables should be lower-case. Environment variables should be upper-case.
– ctrl-alt-delor
yesterday