grep with heredoc in function

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I want to make function that parses from text pasted in a terminal.



$ cat <<eof | grep --color sometext
> foo
> sometext
> sometext1
> a sometext
> asdf
>
> eof
sometext
sometext1
a sometext


While the above works I can make neither an alias nor a function of it.



alias gsi='cat <<eof | grep --color "$1"'
gsi cat <<eof


I thought redirections weren't executed during function definition.










share|improve this question

























    up vote
    2
    down vote

    favorite












    I want to make function that parses from text pasted in a terminal.



    $ cat <<eof | grep --color sometext
    > foo
    > sometext
    > sometext1
    > a sometext
    > asdf
    >
    > eof
    sometext
    sometext1
    a sometext


    While the above works I can make neither an alias nor a function of it.



    alias gsi='cat <<eof | grep --color "$1"'
    gsi cat <<eof


    I thought redirections weren't executed during function definition.










    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I want to make function that parses from text pasted in a terminal.



      $ cat <<eof | grep --color sometext
      > foo
      > sometext
      > sometext1
      > a sometext
      > asdf
      >
      > eof
      sometext
      sometext1
      a sometext


      While the above works I can make neither an alias nor a function of it.



      alias gsi='cat <<eof | grep --color "$1"'
      gsi cat <<eof


      I thought redirections weren't executed during function definition.










      share|improve this question













      I want to make function that parses from text pasted in a terminal.



      $ cat <<eof | grep --color sometext
      > foo
      > sometext
      > sometext1
      > a sometext
      > asdf
      >
      > eof
      sometext
      sometext1
      a sometext


      While the above works I can make neither an alias nor a function of it.



      alias gsi='cat <<eof | grep --color "$1"'
      gsi cat <<eof


      I thought redirections weren't executed during function definition.







      bash here-document bash-functions






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 15 at 14:15









      1.61803

      446615




      446615




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          5
          down vote













          alias 'gsi=<<eof grep --color'


          Would work as alias is just like preprocessor text replacement, where the replacement is interpreted as shell code again.



          Yours was not working as you had that "$1". With gsi file.txt replaced with cat <<eof | grep --color "$1" file.txt, then the shell carries on interpreting that command line and $1 at that point is the shell's first positional parameter which is probably empty unless you did a set something beforehand. So you'd want to remove that "$1" here. You can remove the UUOC as well.



          gsi() cat <<eof 


          is wrong from a syntax point of view as the eof line is missing after that cat <<eof.






          share|improve this answer




















          • Thanks, for emending the alias. I still don't understand why here-document is executed during function definition.
            – 1.61803
            Aug 15 at 16:14










          • @1.61803 it's not that the heredoc is executed, it's that the function body has to be syntactically complete so that bash can understand how much of what you have given is the function, unlike aliases which are essentially simple text replacements.
            – muru
            Aug 16 at 0:31

















          up vote
          4
          down vote













          Your function would not be using a here-document:



          gsi () 
          grep --color "$@"



          This would enable you to pass a pattern as well as any other options to grep on the common line and grep would read from standard input.



          The end of the input would be signalled by pressing Ctrl+D.



          The above would possibly intermingle the pasted contents and the result from grep. To accumulate the pasted content first and then run grep, you could use something like



          gsi () 
          trap 'rm -f "$buffer"' RETURN
          local buffer="$(mktemp)"
          cat >"$buffer" && grep --color "$@" "$buffer"



          Here, the pasted output is put into a temporary file before the grep is run. The temporary file is removed when the function exits. This is approximately what bash would do behind the scenes when you paste into a here-document.






          share|improve this answer


















          • 2




            This works, but is a bit awkward in that if the input is pasted to the terminal, it will get intermixed with output from grep. The here-doc works as a work-around for that, though I suppose one could wrap the grep within stty -echo and stty echo to hide the pasted text...
            – ilkkachu
            Aug 15 at 14:38










          • @ilkkachu True, the here-document in the question would act like a temporary buffer. I'll try your suggestion and make an update when I'm in front of a computer later.
            – Kusalananda
            Aug 15 at 15:03










          • @Kusalananda, ideally, I would have both pasted and greped text on screen.
            – 1.61803
            Aug 15 at 16:04

















          up vote
          2
          down vote













          You mentioned you're pasting to terminal. Abandon here-doc altogether, and use a proper clipboard utility, such as xclip (or pbpaste on Mac OS X). With xclip that'd be done as



          xclip -o -sel clip | grep --color 'sometext'


          See also 'xclip' vs. 'xsel' on the difference between the two utilities.






          share|improve this answer






















          • Thanks for the suggestion, but in this case I rather not use additional software, which by the way isn't that widespread.
            – 1.61803
            Aug 15 at 16:13










          • @1.61803 Understandable. Hopefully, this helps to others. xclip is actually a fairly common software, at least across Linux repositories.
            – Sergiy Kolodyazhnyy
            Aug 15 at 16:16










          • repology.org/metapackage/xclip/versions
            – 1.61803
            Aug 15 at 17:48










          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          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%2f462759%2fgrep-with-heredoc-in-function%23new-answer', 'question_page');

          );

          Post as a guest






























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote













          alias 'gsi=<<eof grep --color'


          Would work as alias is just like preprocessor text replacement, where the replacement is interpreted as shell code again.



          Yours was not working as you had that "$1". With gsi file.txt replaced with cat <<eof | grep --color "$1" file.txt, then the shell carries on interpreting that command line and $1 at that point is the shell's first positional parameter which is probably empty unless you did a set something beforehand. So you'd want to remove that "$1" here. You can remove the UUOC as well.



          gsi() cat <<eof 


          is wrong from a syntax point of view as the eof line is missing after that cat <<eof.






          share|improve this answer




















          • Thanks, for emending the alias. I still don't understand why here-document is executed during function definition.
            – 1.61803
            Aug 15 at 16:14










          • @1.61803 it's not that the heredoc is executed, it's that the function body has to be syntactically complete so that bash can understand how much of what you have given is the function, unlike aliases which are essentially simple text replacements.
            – muru
            Aug 16 at 0:31














          up vote
          5
          down vote













          alias 'gsi=<<eof grep --color'


          Would work as alias is just like preprocessor text replacement, where the replacement is interpreted as shell code again.



          Yours was not working as you had that "$1". With gsi file.txt replaced with cat <<eof | grep --color "$1" file.txt, then the shell carries on interpreting that command line and $1 at that point is the shell's first positional parameter which is probably empty unless you did a set something beforehand. So you'd want to remove that "$1" here. You can remove the UUOC as well.



          gsi() cat <<eof 


          is wrong from a syntax point of view as the eof line is missing after that cat <<eof.






          share|improve this answer




















          • Thanks, for emending the alias. I still don't understand why here-document is executed during function definition.
            – 1.61803
            Aug 15 at 16:14










          • @1.61803 it's not that the heredoc is executed, it's that the function body has to be syntactically complete so that bash can understand how much of what you have given is the function, unlike aliases which are essentially simple text replacements.
            – muru
            Aug 16 at 0:31












          up vote
          5
          down vote










          up vote
          5
          down vote









          alias 'gsi=<<eof grep --color'


          Would work as alias is just like preprocessor text replacement, where the replacement is interpreted as shell code again.



          Yours was not working as you had that "$1". With gsi file.txt replaced with cat <<eof | grep --color "$1" file.txt, then the shell carries on interpreting that command line and $1 at that point is the shell's first positional parameter which is probably empty unless you did a set something beforehand. So you'd want to remove that "$1" here. You can remove the UUOC as well.



          gsi() cat <<eof 


          is wrong from a syntax point of view as the eof line is missing after that cat <<eof.






          share|improve this answer












          alias 'gsi=<<eof grep --color'


          Would work as alias is just like preprocessor text replacement, where the replacement is interpreted as shell code again.



          Yours was not working as you had that "$1". With gsi file.txt replaced with cat <<eof | grep --color "$1" file.txt, then the shell carries on interpreting that command line and $1 at that point is the shell's first positional parameter which is probably empty unless you did a set something beforehand. So you'd want to remove that "$1" here. You can remove the UUOC as well.



          gsi() cat <<eof 


          is wrong from a syntax point of view as the eof line is missing after that cat <<eof.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 15 at 14:26









          Stéphane Chazelas

          285k53524864




          285k53524864











          • Thanks, for emending the alias. I still don't understand why here-document is executed during function definition.
            – 1.61803
            Aug 15 at 16:14










          • @1.61803 it's not that the heredoc is executed, it's that the function body has to be syntactically complete so that bash can understand how much of what you have given is the function, unlike aliases which are essentially simple text replacements.
            – muru
            Aug 16 at 0:31
















          • Thanks, for emending the alias. I still don't understand why here-document is executed during function definition.
            – 1.61803
            Aug 15 at 16:14










          • @1.61803 it's not that the heredoc is executed, it's that the function body has to be syntactically complete so that bash can understand how much of what you have given is the function, unlike aliases which are essentially simple text replacements.
            – muru
            Aug 16 at 0:31















          Thanks, for emending the alias. I still don't understand why here-document is executed during function definition.
          – 1.61803
          Aug 15 at 16:14




          Thanks, for emending the alias. I still don't understand why here-document is executed during function definition.
          – 1.61803
          Aug 15 at 16:14












          @1.61803 it's not that the heredoc is executed, it's that the function body has to be syntactically complete so that bash can understand how much of what you have given is the function, unlike aliases which are essentially simple text replacements.
          – muru
          Aug 16 at 0:31




          @1.61803 it's not that the heredoc is executed, it's that the function body has to be syntactically complete so that bash can understand how much of what you have given is the function, unlike aliases which are essentially simple text replacements.
          – muru
          Aug 16 at 0:31












          up vote
          4
          down vote













          Your function would not be using a here-document:



          gsi () 
          grep --color "$@"



          This would enable you to pass a pattern as well as any other options to grep on the common line and grep would read from standard input.



          The end of the input would be signalled by pressing Ctrl+D.



          The above would possibly intermingle the pasted contents and the result from grep. To accumulate the pasted content first and then run grep, you could use something like



          gsi () 
          trap 'rm -f "$buffer"' RETURN
          local buffer="$(mktemp)"
          cat >"$buffer" && grep --color "$@" "$buffer"



          Here, the pasted output is put into a temporary file before the grep is run. The temporary file is removed when the function exits. This is approximately what bash would do behind the scenes when you paste into a here-document.






          share|improve this answer


















          • 2




            This works, but is a bit awkward in that if the input is pasted to the terminal, it will get intermixed with output from grep. The here-doc works as a work-around for that, though I suppose one could wrap the grep within stty -echo and stty echo to hide the pasted text...
            – ilkkachu
            Aug 15 at 14:38










          • @ilkkachu True, the here-document in the question would act like a temporary buffer. I'll try your suggestion and make an update when I'm in front of a computer later.
            – Kusalananda
            Aug 15 at 15:03










          • @Kusalananda, ideally, I would have both pasted and greped text on screen.
            – 1.61803
            Aug 15 at 16:04














          up vote
          4
          down vote













          Your function would not be using a here-document:



          gsi () 
          grep --color "$@"



          This would enable you to pass a pattern as well as any other options to grep on the common line and grep would read from standard input.



          The end of the input would be signalled by pressing Ctrl+D.



          The above would possibly intermingle the pasted contents and the result from grep. To accumulate the pasted content first and then run grep, you could use something like



          gsi () 
          trap 'rm -f "$buffer"' RETURN
          local buffer="$(mktemp)"
          cat >"$buffer" && grep --color "$@" "$buffer"



          Here, the pasted output is put into a temporary file before the grep is run. The temporary file is removed when the function exits. This is approximately what bash would do behind the scenes when you paste into a here-document.






          share|improve this answer


















          • 2




            This works, but is a bit awkward in that if the input is pasted to the terminal, it will get intermixed with output from grep. The here-doc works as a work-around for that, though I suppose one could wrap the grep within stty -echo and stty echo to hide the pasted text...
            – ilkkachu
            Aug 15 at 14:38










          • @ilkkachu True, the here-document in the question would act like a temporary buffer. I'll try your suggestion and make an update when I'm in front of a computer later.
            – Kusalananda
            Aug 15 at 15:03










          • @Kusalananda, ideally, I would have both pasted and greped text on screen.
            – 1.61803
            Aug 15 at 16:04












          up vote
          4
          down vote










          up vote
          4
          down vote









          Your function would not be using a here-document:



          gsi () 
          grep --color "$@"



          This would enable you to pass a pattern as well as any other options to grep on the common line and grep would read from standard input.



          The end of the input would be signalled by pressing Ctrl+D.



          The above would possibly intermingle the pasted contents and the result from grep. To accumulate the pasted content first and then run grep, you could use something like



          gsi () 
          trap 'rm -f "$buffer"' RETURN
          local buffer="$(mktemp)"
          cat >"$buffer" && grep --color "$@" "$buffer"



          Here, the pasted output is put into a temporary file before the grep is run. The temporary file is removed when the function exits. This is approximately what bash would do behind the scenes when you paste into a here-document.






          share|improve this answer














          Your function would not be using a here-document:



          gsi () 
          grep --color "$@"



          This would enable you to pass a pattern as well as any other options to grep on the common line and grep would read from standard input.



          The end of the input would be signalled by pressing Ctrl+D.



          The above would possibly intermingle the pasted contents and the result from grep. To accumulate the pasted content first and then run grep, you could use something like



          gsi () 
          trap 'rm -f "$buffer"' RETURN
          local buffer="$(mktemp)"
          cat >"$buffer" && grep --color "$@" "$buffer"



          Here, the pasted output is put into a temporary file before the grep is run. The temporary file is removed when the function exits. This is approximately what bash would do behind the scenes when you paste into a here-document.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 15 at 16:40

























          answered Aug 15 at 14:30









          Kusalananda

          106k14209327




          106k14209327







          • 2




            This works, but is a bit awkward in that if the input is pasted to the terminal, it will get intermixed with output from grep. The here-doc works as a work-around for that, though I suppose one could wrap the grep within stty -echo and stty echo to hide the pasted text...
            – ilkkachu
            Aug 15 at 14:38










          • @ilkkachu True, the here-document in the question would act like a temporary buffer. I'll try your suggestion and make an update when I'm in front of a computer later.
            – Kusalananda
            Aug 15 at 15:03










          • @Kusalananda, ideally, I would have both pasted and greped text on screen.
            – 1.61803
            Aug 15 at 16:04












          • 2




            This works, but is a bit awkward in that if the input is pasted to the terminal, it will get intermixed with output from grep. The here-doc works as a work-around for that, though I suppose one could wrap the grep within stty -echo and stty echo to hide the pasted text...
            – ilkkachu
            Aug 15 at 14:38










          • @ilkkachu True, the here-document in the question would act like a temporary buffer. I'll try your suggestion and make an update when I'm in front of a computer later.
            – Kusalananda
            Aug 15 at 15:03










          • @Kusalananda, ideally, I would have both pasted and greped text on screen.
            – 1.61803
            Aug 15 at 16:04







          2




          2




          This works, but is a bit awkward in that if the input is pasted to the terminal, it will get intermixed with output from grep. The here-doc works as a work-around for that, though I suppose one could wrap the grep within stty -echo and stty echo to hide the pasted text...
          – ilkkachu
          Aug 15 at 14:38




          This works, but is a bit awkward in that if the input is pasted to the terminal, it will get intermixed with output from grep. The here-doc works as a work-around for that, though I suppose one could wrap the grep within stty -echo and stty echo to hide the pasted text...
          – ilkkachu
          Aug 15 at 14:38












          @ilkkachu True, the here-document in the question would act like a temporary buffer. I'll try your suggestion and make an update when I'm in front of a computer later.
          – Kusalananda
          Aug 15 at 15:03




          @ilkkachu True, the here-document in the question would act like a temporary buffer. I'll try your suggestion and make an update when I'm in front of a computer later.
          – Kusalananda
          Aug 15 at 15:03












          @Kusalananda, ideally, I would have both pasted and greped text on screen.
          – 1.61803
          Aug 15 at 16:04




          @Kusalananda, ideally, I would have both pasted and greped text on screen.
          – 1.61803
          Aug 15 at 16:04










          up vote
          2
          down vote













          You mentioned you're pasting to terminal. Abandon here-doc altogether, and use a proper clipboard utility, such as xclip (or pbpaste on Mac OS X). With xclip that'd be done as



          xclip -o -sel clip | grep --color 'sometext'


          See also 'xclip' vs. 'xsel' on the difference between the two utilities.






          share|improve this answer






















          • Thanks for the suggestion, but in this case I rather not use additional software, which by the way isn't that widespread.
            – 1.61803
            Aug 15 at 16:13










          • @1.61803 Understandable. Hopefully, this helps to others. xclip is actually a fairly common software, at least across Linux repositories.
            – Sergiy Kolodyazhnyy
            Aug 15 at 16:16










          • repology.org/metapackage/xclip/versions
            – 1.61803
            Aug 15 at 17:48














          up vote
          2
          down vote













          You mentioned you're pasting to terminal. Abandon here-doc altogether, and use a proper clipboard utility, such as xclip (or pbpaste on Mac OS X). With xclip that'd be done as



          xclip -o -sel clip | grep --color 'sometext'


          See also 'xclip' vs. 'xsel' on the difference between the two utilities.






          share|improve this answer






















          • Thanks for the suggestion, but in this case I rather not use additional software, which by the way isn't that widespread.
            – 1.61803
            Aug 15 at 16:13










          • @1.61803 Understandable. Hopefully, this helps to others. xclip is actually a fairly common software, at least across Linux repositories.
            – Sergiy Kolodyazhnyy
            Aug 15 at 16:16










          • repology.org/metapackage/xclip/versions
            – 1.61803
            Aug 15 at 17:48












          up vote
          2
          down vote










          up vote
          2
          down vote









          You mentioned you're pasting to terminal. Abandon here-doc altogether, and use a proper clipboard utility, such as xclip (or pbpaste on Mac OS X). With xclip that'd be done as



          xclip -o -sel clip | grep --color 'sometext'


          See also 'xclip' vs. 'xsel' on the difference between the two utilities.






          share|improve this answer














          You mentioned you're pasting to terminal. Abandon here-doc altogether, and use a proper clipboard utility, such as xclip (or pbpaste on Mac OS X). With xclip that'd be done as



          xclip -o -sel clip | grep --color 'sometext'


          See also 'xclip' vs. 'xsel' on the difference between the two utilities.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 15 at 16:27

























          answered Aug 15 at 16:07









          Sergiy Kolodyazhnyy

          7,93511648




          7,93511648











          • Thanks for the suggestion, but in this case I rather not use additional software, which by the way isn't that widespread.
            – 1.61803
            Aug 15 at 16:13










          • @1.61803 Understandable. Hopefully, this helps to others. xclip is actually a fairly common software, at least across Linux repositories.
            – Sergiy Kolodyazhnyy
            Aug 15 at 16:16










          • repology.org/metapackage/xclip/versions
            – 1.61803
            Aug 15 at 17:48
















          • Thanks for the suggestion, but in this case I rather not use additional software, which by the way isn't that widespread.
            – 1.61803
            Aug 15 at 16:13










          • @1.61803 Understandable. Hopefully, this helps to others. xclip is actually a fairly common software, at least across Linux repositories.
            – Sergiy Kolodyazhnyy
            Aug 15 at 16:16










          • repology.org/metapackage/xclip/versions
            – 1.61803
            Aug 15 at 17:48















          Thanks for the suggestion, but in this case I rather not use additional software, which by the way isn't that widespread.
          – 1.61803
          Aug 15 at 16:13




          Thanks for the suggestion, but in this case I rather not use additional software, which by the way isn't that widespread.
          – 1.61803
          Aug 15 at 16:13












          @1.61803 Understandable. Hopefully, this helps to others. xclip is actually a fairly common software, at least across Linux repositories.
          – Sergiy Kolodyazhnyy
          Aug 15 at 16:16




          @1.61803 Understandable. Hopefully, this helps to others. xclip is actually a fairly common software, at least across Linux repositories.
          – Sergiy Kolodyazhnyy
          Aug 15 at 16:16












          repology.org/metapackage/xclip/versions
          – 1.61803
          Aug 15 at 17:48




          repology.org/metapackage/xclip/versions
          – 1.61803
          Aug 15 at 17:48

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462759%2fgrep-with-heredoc-in-function%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?