Why doesn't this work: ~$ echo “trythis” | sh ./script.shWhere I'm making the mistake in here document?Bash script to move filesWhy my shell script doesn't open the terminal?How to convert a string into an array in bash script?How can I use a pipe in a while condition?Moving files from source to destination in categoriesWhy is my bash script getting “Killed”? How do I prevent that?Bash script to create a directorymysqldump script doesn't work in cronBash script not running all comands

What is a simple, physical situation where complex numbers emerge naturally?

Do adult Russians normally hand-write Cyrillic as cursive or as block letters?

Is it possible to kill all life on Earth?

Why don't I have ground wiring on any of my outlets?

How to decline physical affection from a child whose parents are pressuring them?

Are grass strips more dangerous than tarmac?

How much current can Baofeng UV-5R provide on +V pin?

what's the equivalent of helper in LWC?

Working in the USA for living expenses only; allowed on VWP?

Can a magnetic field of a large body be stronger than its gravity?

Explain Ant-Man's "not it" scene from Avengers: Endgame

California: "For quality assurance, this phone call is being recorded"

How to detach yourself from a character you're going to kill?

What people are called "кабан" and why?

Can you use a concentration spell while using Mantle of Majesty?

What do you call the small burst of laugh that people let out when they want to refrain from laughing, but can't?

Future enhancements for the finite element method

How to write a vulnerable moment without it seeming cliche or mushy?

Comma Code - Ch. 4 Automate the Boring Stuff

Looking after a wayward brother in mother's will

Is evaporation a kind of phase transition?

Accidentally cashed a check twice

Relativistic resistance transformation

Humans meet a distant alien species. How do they standardize? - Units of Measure



Why doesn't this work: ~$ echo “trythis” | sh ./script.sh


Where I'm making the mistake in here document?Bash script to move filesWhy my shell script doesn't open the terminal?How to convert a string into an array in bash script?How can I use a pipe in a while condition?Moving files from source to destination in categoriesWhy is my bash script getting “Killed”? How do I prevent that?Bash script to create a directorymysqldump script doesn't work in cronBash script not running all comands






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I'm trying to feed the string trythis into the script I made that creates a directory using what is written after it.
For example this works:



~$ sh ./script trythis


But using the pipe does not.
I am rather new to scripting so still getting the hang of various concepts.










share|improve this question
























  • Why did you expect it to work?

    – fkraiem
    Apr 14 at 19:08











  • See this question

    – Elias
    Apr 14 at 19:09






  • 1





    I was going to add to my answer whole lot of extra information about how things work behind the scenes, but considering you're somewhat new to scripting I've put that aside for now. Too much information can be overwhelming. Please let me know if you do what extra clarifications on any of the things in my answer

    – Sergiy Kolodyazhnyy
    Apr 14 at 19:42


















0















I'm trying to feed the string trythis into the script I made that creates a directory using what is written after it.
For example this works:



~$ sh ./script trythis


But using the pipe does not.
I am rather new to scripting so still getting the hang of various concepts.










share|improve this question
























  • Why did you expect it to work?

    – fkraiem
    Apr 14 at 19:08











  • See this question

    – Elias
    Apr 14 at 19:09






  • 1





    I was going to add to my answer whole lot of extra information about how things work behind the scenes, but considering you're somewhat new to scripting I've put that aside for now. Too much information can be overwhelming. Please let me know if you do what extra clarifications on any of the things in my answer

    – Sergiy Kolodyazhnyy
    Apr 14 at 19:42














0












0








0








I'm trying to feed the string trythis into the script I made that creates a directory using what is written after it.
For example this works:



~$ sh ./script trythis


But using the pipe does not.
I am rather new to scripting so still getting the hang of various concepts.










share|improve this question
















I'm trying to feed the string trythis into the script I made that creates a directory using what is written after it.
For example this works:



~$ sh ./script trythis


But using the pipe does not.
I am rather new to scripting so still getting the hang of various concepts.







command-line scripts pipe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 14 at 19:40









Sergiy Kolodyazhnyy

76.2k10161336




76.2k10161336










asked Apr 14 at 19:05









David David

82




82












  • Why did you expect it to work?

    – fkraiem
    Apr 14 at 19:08











  • See this question

    – Elias
    Apr 14 at 19:09






  • 1





    I was going to add to my answer whole lot of extra information about how things work behind the scenes, but considering you're somewhat new to scripting I've put that aside for now. Too much information can be overwhelming. Please let me know if you do what extra clarifications on any of the things in my answer

    – Sergiy Kolodyazhnyy
    Apr 14 at 19:42


















  • Why did you expect it to work?

    – fkraiem
    Apr 14 at 19:08











  • See this question

    – Elias
    Apr 14 at 19:09






  • 1





    I was going to add to my answer whole lot of extra information about how things work behind the scenes, but considering you're somewhat new to scripting I've put that aside for now. Too much information can be overwhelming. Please let me know if you do what extra clarifications on any of the things in my answer

    – Sergiy Kolodyazhnyy
    Apr 14 at 19:42

















Why did you expect it to work?

– fkraiem
Apr 14 at 19:08





Why did you expect it to work?

– fkraiem
Apr 14 at 19:08













See this question

– Elias
Apr 14 at 19:09





See this question

– Elias
Apr 14 at 19:09




1




1





I was going to add to my answer whole lot of extra information about how things work behind the scenes, but considering you're somewhat new to scripting I've put that aside for now. Too much information can be overwhelming. Please let me know if you do what extra clarifications on any of the things in my answer

– Sergiy Kolodyazhnyy
Apr 14 at 19:42






I was going to add to my answer whole lot of extra information about how things work behind the scenes, but considering you're somewhat new to scripting I've put that aside for now. Too much information can be overwhelming. Please let me know if you do what extra clarifications on any of the things in my answer

– Sergiy Kolodyazhnyy
Apr 14 at 19:42











1 Answer
1






active

oldest

votes


















2














Because pipe and positional parameters are different things



Pipes connect stdin stream of right most command in pipeline to the stdout of left most command. The effect is such as if one command becomes keyboard-like input to the other.



By contrast positional parameters as in



myscript.sh trythis


create an array of values, where myscript and trythis are items in that array, and there is no connection to stdin stream at all



What you should do in such cases is use xargs



echo trythis | xargs myscript 


The xargs command will run the myscript command with trythis added to the front as if done manually.



In other words, exactly because sh ./script trythis expects trythis to be there in positional parameter list you don't see echo trythis | sh ./myscript working. If you did echo try this | sh ./myscript trythis it would work, because now you have positional parameter list, and echo technically becomes redundant.



Of course, it is the application's own decision on what to do with either stdin or positional parameters. Commands such as echo don't care about stdin but only about positional parameters, whereas cat would care about both. If you were to build a script in such way that reads stdin AND accepts positional parameters, then it could also work, for instance grep and tail do that. And exactly in those cases where we want to dynamically create positional parameters or the application ignores stdin - there is the right place to use xargs.






share|improve this answer

























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2faskubuntu.com%2fquestions%2f1133881%2fwhy-doesnt-this-work-echo-trythis-sh-script-sh%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














    Because pipe and positional parameters are different things



    Pipes connect stdin stream of right most command in pipeline to the stdout of left most command. The effect is such as if one command becomes keyboard-like input to the other.



    By contrast positional parameters as in



    myscript.sh trythis


    create an array of values, where myscript and trythis are items in that array, and there is no connection to stdin stream at all



    What you should do in such cases is use xargs



    echo trythis | xargs myscript 


    The xargs command will run the myscript command with trythis added to the front as if done manually.



    In other words, exactly because sh ./script trythis expects trythis to be there in positional parameter list you don't see echo trythis | sh ./myscript working. If you did echo try this | sh ./myscript trythis it would work, because now you have positional parameter list, and echo technically becomes redundant.



    Of course, it is the application's own decision on what to do with either stdin or positional parameters. Commands such as echo don't care about stdin but only about positional parameters, whereas cat would care about both. If you were to build a script in such way that reads stdin AND accepts positional parameters, then it could also work, for instance grep and tail do that. And exactly in those cases where we want to dynamically create positional parameters or the application ignores stdin - there is the right place to use xargs.






    share|improve this answer





























      2














      Because pipe and positional parameters are different things



      Pipes connect stdin stream of right most command in pipeline to the stdout of left most command. The effect is such as if one command becomes keyboard-like input to the other.



      By contrast positional parameters as in



      myscript.sh trythis


      create an array of values, where myscript and trythis are items in that array, and there is no connection to stdin stream at all



      What you should do in such cases is use xargs



      echo trythis | xargs myscript 


      The xargs command will run the myscript command with trythis added to the front as if done manually.



      In other words, exactly because sh ./script trythis expects trythis to be there in positional parameter list you don't see echo trythis | sh ./myscript working. If you did echo try this | sh ./myscript trythis it would work, because now you have positional parameter list, and echo technically becomes redundant.



      Of course, it is the application's own decision on what to do with either stdin or positional parameters. Commands such as echo don't care about stdin but only about positional parameters, whereas cat would care about both. If you were to build a script in such way that reads stdin AND accepts positional parameters, then it could also work, for instance grep and tail do that. And exactly in those cases where we want to dynamically create positional parameters or the application ignores stdin - there is the right place to use xargs.






      share|improve this answer



























        2












        2








        2







        Because pipe and positional parameters are different things



        Pipes connect stdin stream of right most command in pipeline to the stdout of left most command. The effect is such as if one command becomes keyboard-like input to the other.



        By contrast positional parameters as in



        myscript.sh trythis


        create an array of values, where myscript and trythis are items in that array, and there is no connection to stdin stream at all



        What you should do in such cases is use xargs



        echo trythis | xargs myscript 


        The xargs command will run the myscript command with trythis added to the front as if done manually.



        In other words, exactly because sh ./script trythis expects trythis to be there in positional parameter list you don't see echo trythis | sh ./myscript working. If you did echo try this | sh ./myscript trythis it would work, because now you have positional parameter list, and echo technically becomes redundant.



        Of course, it is the application's own decision on what to do with either stdin or positional parameters. Commands such as echo don't care about stdin but only about positional parameters, whereas cat would care about both. If you were to build a script in such way that reads stdin AND accepts positional parameters, then it could also work, for instance grep and tail do that. And exactly in those cases where we want to dynamically create positional parameters or the application ignores stdin - there is the right place to use xargs.






        share|improve this answer















        Because pipe and positional parameters are different things



        Pipes connect stdin stream of right most command in pipeline to the stdout of left most command. The effect is such as if one command becomes keyboard-like input to the other.



        By contrast positional parameters as in



        myscript.sh trythis


        create an array of values, where myscript and trythis are items in that array, and there is no connection to stdin stream at all



        What you should do in such cases is use xargs



        echo trythis | xargs myscript 


        The xargs command will run the myscript command with trythis added to the front as if done manually.



        In other words, exactly because sh ./script trythis expects trythis to be there in positional parameter list you don't see echo trythis | sh ./myscript working. If you did echo try this | sh ./myscript trythis it would work, because now you have positional parameter list, and echo technically becomes redundant.



        Of course, it is the application's own decision on what to do with either stdin or positional parameters. Commands such as echo don't care about stdin but only about positional parameters, whereas cat would care about both. If you were to build a script in such way that reads stdin AND accepts positional parameters, then it could also work, for instance grep and tail do that. And exactly in those cases where we want to dynamically create positional parameters or the application ignores stdin - there is the right place to use xargs.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 14 at 19:32

























        answered Apr 14 at 19:14









        Sergiy KolodyazhnyySergiy Kolodyazhnyy

        76.2k10161336




        76.2k10161336



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Ask Ubuntu!


            • 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%2faskubuntu.com%2fquestions%2f1133881%2fwhy-doesnt-this-work-echo-trythis-sh-script-sh%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

            Tamil (spriik) Luke uk diar | Nawigatjuun

            Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

            Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?