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










1















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?










share|improve this question






















  • As a side note, shell variables should be lower-case. Environment variables should be upper-case.

    – ctrl-alt-delor
    yesterday
















1















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?










share|improve this question






















  • As a side note, shell variables should be lower-case. Environment variables should be upper-case.

    – ctrl-alt-delor
    yesterday














1












1








1








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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











1 Answer
1






active

oldest

votes


















2















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.




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.






share|improve this answer
























    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
    );



    );













    draft saved

    draft discarded


















    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









    2















    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.




    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.






    share|improve this answer





























      2















      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.




      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.






      share|improve this answer



























        2












        2








        2








        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.




        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.






        share|improve this answer
















        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.




        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        ilkkachuilkkachu

        62.5k10103179




        62.5k10103179



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

            Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

            Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.