Redirect to file, including current prompt line









up vote
4
down vote

favorite
1












How to include the redirected command itself to the output file redirection? For example



echo "hello!">output.txt


I want the content of output file like this:



echo "hello!">output.txt
hello!









share|improve this question























  • Do you have a specific need for this?
    – wjandrea
    Nov 10 at 19:15











  • Yes. I have a specific need. I am creating a log file.
    – mandrake00
    Nov 13 at 11:39














up vote
4
down vote

favorite
1












How to include the redirected command itself to the output file redirection? For example



echo "hello!">output.txt


I want the content of output file like this:



echo "hello!">output.txt
hello!









share|improve this question























  • Do you have a specific need for this?
    – wjandrea
    Nov 10 at 19:15











  • Yes. I have a specific need. I am creating a log file.
    – mandrake00
    Nov 13 at 11:39












up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1





How to include the redirected command itself to the output file redirection? For example



echo "hello!">output.txt


I want the content of output file like this:



echo "hello!">output.txt
hello!









share|improve this question















How to include the redirected command itself to the output file redirection? For example



echo "hello!">output.txt


I want the content of output file like this:



echo "hello!">output.txt
hello!






io-redirection






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 14:43









ctrl-alt-delor

9,93831954




9,93831954










asked Nov 10 at 14:11









mandrake00

244




244











  • Do you have a specific need for this?
    – wjandrea
    Nov 10 at 19:15











  • Yes. I have a specific need. I am creating a log file.
    – mandrake00
    Nov 13 at 11:39
















  • Do you have a specific need for this?
    – wjandrea
    Nov 10 at 19:15











  • Yes. I have a specific need. I am creating a log file.
    – mandrake00
    Nov 13 at 11:39















Do you have a specific need for this?
– wjandrea
Nov 10 at 19:15





Do you have a specific need for this?
– wjandrea
Nov 10 at 19:15













Yes. I have a specific need. I am creating a log file.
– mandrake00
Nov 13 at 11:39




Yes. I have a specific need. I am creating a log file.
– mandrake00
Nov 13 at 11:39










4 Answers
4






active

oldest

votes

















up vote
12
down vote













You may want one of two things here.




  1. Using tee to get the result of the echo sent to the terminal as well as to the file:



    $ echo 'hello!' | tee output
    hello!
    $ cat output
    hello!



  2. Using script to capture the whole terminal session:



    $ script
    Script started, output file is typescript
    $ echo 'hello!' >output
    $ cat output
    hello!
    $ exit
    Script done, output file is typescript




    $ cat typescript
    Script started on Sat Nov 10 15:15:06 2018
    $ echo 'hello!' >output
    $ cat output
    hello!
    $ exit

    Script done on Sat Nov 10 15:15:37 2018


If this is not what you are asking for, then please clarify your question.






share|improve this answer






















  • Hi! Nanda, Thanks. I know tee. But I want the output file to include the redirect command that we type in, to be in the output file as well. When I do "cat output", I want to see "echo 'helo'>output" before the line "helo". Hope you understood.
    – mandrake00
    Nov 10 at 14:26










  • @mandrake00 isn't that exactly what option 2 gives you? script should do what you need.
    – terdon
    Nov 10 at 14:37










  • Thank you. The typescript output file has many unwanted lines such as "script started at ..." and "<cr><cr> etc. I was looking for a simple method so that when I do echo 'helo'>out.txt this would give a file with just 2 lines, i.e. echo 'helo'>out.txt n helo
    – mandrake00
    Nov 10 at 14:45






  • 1




    Do you want this to happen for every single command you ever type in any terminal? Just for specific commands? Please edit your question and give us more specific requirements.
    – terdon
    Nov 10 at 14:46






  • 4




    You can suppress the lines about when it was started and stopped by using script -q
    – Panki
    Nov 10 at 14:56

















up vote
5
down vote













In the genearality you're asking for, this is not possible.



To achieve this with additional tools (akin to script(1)), it would be necessary for that program, observing your shell, that in



command >foo


foo is the name of a file, and create it. Also, the shell would try to create it, so there's conflict already. Also, what if the program would print



command >foo


to the terminal?



From inside the shell, well, the shell's redirection process is extremely primitive from a programming point of view (try to find a C lecture where they build a primitive shell themselves, it is really surprising).



If you really need this, you would have to hack your own shell to intercept redirections and pass the command into the target file. But there are probably a lot of nasty issues in the details...






share|improve this answer



























    up vote
    4
    down vote













    You can also pipe the whole shell:



    $ sh -i |& tee sh.log
    sh-4.4$ hello
    sh: hello: command not found
    sh-4.4$ echo hi
    hi
    sh-4.4$ exit


    -i is needed to keep the shell interactive despite stdout not being a terminal. bash and zsh also support that option. |& pipes stdout and stderr; it works with zsh and bash, but not sh (there, you'd need 2>&1 |). Of course, you could also use &> or 2>&1 > if you just want to redirect to a file and nothing more. Anyways, sh.log here contains everything.



    $ cat sh.log
    sh-4.4$ hello
    sh: hello: command not found
    sh-4.4$ echo hi
    hi
    sh-4.4$ exit





    share|improve this answer





























      up vote
      2
      down vote













      If it's acceptable for you to drop the output filename, I think the following is the easiest way:



      command='echo "hello!"'
      (echo "$command"; eval "$command") >output.txt


      But if you need to print the filename then you could use the following:



      command='echo "hello!"'
      (echo "$command >output.txt"; eval "$command") >output.txt


      You can introduce a variable for the filename in order to make sure that it is the same in the two occurrences:



      output="output.txt"
      command='echo "hello!"'
      (echo "$command >$output"; eval "$command") >$output


      This way you avoid the risk of having a difference in the printed filename and the actual filename.






      share|improve this answer


















      • 1




        Generalizing as a function: f() command="$1"; file="$2"; (printf '%s >%s' "$command" "$file"; eval "$command") >"$file"; Then call: f 'echo "Hello!"' output.txt
        – wjandrea
        Nov 10 at 19:20










      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: 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%2f480956%2fredirect-to-file-including-current-prompt-line%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      12
      down vote













      You may want one of two things here.




      1. Using tee to get the result of the echo sent to the terminal as well as to the file:



        $ echo 'hello!' | tee output
        hello!
        $ cat output
        hello!



      2. Using script to capture the whole terminal session:



        $ script
        Script started, output file is typescript
        $ echo 'hello!' >output
        $ cat output
        hello!
        $ exit
        Script done, output file is typescript




        $ cat typescript
        Script started on Sat Nov 10 15:15:06 2018
        $ echo 'hello!' >output
        $ cat output
        hello!
        $ exit

        Script done on Sat Nov 10 15:15:37 2018


      If this is not what you are asking for, then please clarify your question.






      share|improve this answer






















      • Hi! Nanda, Thanks. I know tee. But I want the output file to include the redirect command that we type in, to be in the output file as well. When I do "cat output", I want to see "echo 'helo'>output" before the line "helo". Hope you understood.
        – mandrake00
        Nov 10 at 14:26










      • @mandrake00 isn't that exactly what option 2 gives you? script should do what you need.
        – terdon
        Nov 10 at 14:37










      • Thank you. The typescript output file has many unwanted lines such as "script started at ..." and "<cr><cr> etc. I was looking for a simple method so that when I do echo 'helo'>out.txt this would give a file with just 2 lines, i.e. echo 'helo'>out.txt n helo
        – mandrake00
        Nov 10 at 14:45






      • 1




        Do you want this to happen for every single command you ever type in any terminal? Just for specific commands? Please edit your question and give us more specific requirements.
        – terdon
        Nov 10 at 14:46






      • 4




        You can suppress the lines about when it was started and stopped by using script -q
        – Panki
        Nov 10 at 14:56














      up vote
      12
      down vote













      You may want one of two things here.




      1. Using tee to get the result of the echo sent to the terminal as well as to the file:



        $ echo 'hello!' | tee output
        hello!
        $ cat output
        hello!



      2. Using script to capture the whole terminal session:



        $ script
        Script started, output file is typescript
        $ echo 'hello!' >output
        $ cat output
        hello!
        $ exit
        Script done, output file is typescript




        $ cat typescript
        Script started on Sat Nov 10 15:15:06 2018
        $ echo 'hello!' >output
        $ cat output
        hello!
        $ exit

        Script done on Sat Nov 10 15:15:37 2018


      If this is not what you are asking for, then please clarify your question.






      share|improve this answer






















      • Hi! Nanda, Thanks. I know tee. But I want the output file to include the redirect command that we type in, to be in the output file as well. When I do "cat output", I want to see "echo 'helo'>output" before the line "helo". Hope you understood.
        – mandrake00
        Nov 10 at 14:26










      • @mandrake00 isn't that exactly what option 2 gives you? script should do what you need.
        – terdon
        Nov 10 at 14:37










      • Thank you. The typescript output file has many unwanted lines such as "script started at ..." and "<cr><cr> etc. I was looking for a simple method so that when I do echo 'helo'>out.txt this would give a file with just 2 lines, i.e. echo 'helo'>out.txt n helo
        – mandrake00
        Nov 10 at 14:45






      • 1




        Do you want this to happen for every single command you ever type in any terminal? Just for specific commands? Please edit your question and give us more specific requirements.
        – terdon
        Nov 10 at 14:46






      • 4




        You can suppress the lines about when it was started and stopped by using script -q
        – Panki
        Nov 10 at 14:56












      up vote
      12
      down vote










      up vote
      12
      down vote









      You may want one of two things here.




      1. Using tee to get the result of the echo sent to the terminal as well as to the file:



        $ echo 'hello!' | tee output
        hello!
        $ cat output
        hello!



      2. Using script to capture the whole terminal session:



        $ script
        Script started, output file is typescript
        $ echo 'hello!' >output
        $ cat output
        hello!
        $ exit
        Script done, output file is typescript




        $ cat typescript
        Script started on Sat Nov 10 15:15:06 2018
        $ echo 'hello!' >output
        $ cat output
        hello!
        $ exit

        Script done on Sat Nov 10 15:15:37 2018


      If this is not what you are asking for, then please clarify your question.






      share|improve this answer














      You may want one of two things here.




      1. Using tee to get the result of the echo sent to the terminal as well as to the file:



        $ echo 'hello!' | tee output
        hello!
        $ cat output
        hello!



      2. Using script to capture the whole terminal session:



        $ script
        Script started, output file is typescript
        $ echo 'hello!' >output
        $ cat output
        hello!
        $ exit
        Script done, output file is typescript




        $ cat typescript
        Script started on Sat Nov 10 15:15:06 2018
        $ echo 'hello!' >output
        $ cat output
        hello!
        $ exit

        Script done on Sat Nov 10 15:15:37 2018


      If this is not what you are asking for, then please clarify your question.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 10 at 16:21









      Cristian Ciupitu

      2,05911621




      2,05911621










      answered Nov 10 at 14:20









      Kusalananda

      116k15218351




      116k15218351











      • Hi! Nanda, Thanks. I know tee. But I want the output file to include the redirect command that we type in, to be in the output file as well. When I do "cat output", I want to see "echo 'helo'>output" before the line "helo". Hope you understood.
        – mandrake00
        Nov 10 at 14:26










      • @mandrake00 isn't that exactly what option 2 gives you? script should do what you need.
        – terdon
        Nov 10 at 14:37










      • Thank you. The typescript output file has many unwanted lines such as "script started at ..." and "<cr><cr> etc. I was looking for a simple method so that when I do echo 'helo'>out.txt this would give a file with just 2 lines, i.e. echo 'helo'>out.txt n helo
        – mandrake00
        Nov 10 at 14:45






      • 1




        Do you want this to happen for every single command you ever type in any terminal? Just for specific commands? Please edit your question and give us more specific requirements.
        – terdon
        Nov 10 at 14:46






      • 4




        You can suppress the lines about when it was started and stopped by using script -q
        – Panki
        Nov 10 at 14:56
















      • Hi! Nanda, Thanks. I know tee. But I want the output file to include the redirect command that we type in, to be in the output file as well. When I do "cat output", I want to see "echo 'helo'>output" before the line "helo". Hope you understood.
        – mandrake00
        Nov 10 at 14:26










      • @mandrake00 isn't that exactly what option 2 gives you? script should do what you need.
        – terdon
        Nov 10 at 14:37










      • Thank you. The typescript output file has many unwanted lines such as "script started at ..." and "<cr><cr> etc. I was looking for a simple method so that when I do echo 'helo'>out.txt this would give a file with just 2 lines, i.e. echo 'helo'>out.txt n helo
        – mandrake00
        Nov 10 at 14:45






      • 1




        Do you want this to happen for every single command you ever type in any terminal? Just for specific commands? Please edit your question and give us more specific requirements.
        – terdon
        Nov 10 at 14:46






      • 4




        You can suppress the lines about when it was started and stopped by using script -q
        – Panki
        Nov 10 at 14:56















      Hi! Nanda, Thanks. I know tee. But I want the output file to include the redirect command that we type in, to be in the output file as well. When I do "cat output", I want to see "echo 'helo'>output" before the line "helo". Hope you understood.
      – mandrake00
      Nov 10 at 14:26




      Hi! Nanda, Thanks. I know tee. But I want the output file to include the redirect command that we type in, to be in the output file as well. When I do "cat output", I want to see "echo 'helo'>output" before the line "helo". Hope you understood.
      – mandrake00
      Nov 10 at 14:26












      @mandrake00 isn't that exactly what option 2 gives you? script should do what you need.
      – terdon
      Nov 10 at 14:37




      @mandrake00 isn't that exactly what option 2 gives you? script should do what you need.
      – terdon
      Nov 10 at 14:37












      Thank you. The typescript output file has many unwanted lines such as "script started at ..." and "<cr><cr> etc. I was looking for a simple method so that when I do echo 'helo'>out.txt this would give a file with just 2 lines, i.e. echo 'helo'>out.txt n helo
      – mandrake00
      Nov 10 at 14:45




      Thank you. The typescript output file has many unwanted lines such as "script started at ..." and "<cr><cr> etc. I was looking for a simple method so that when I do echo 'helo'>out.txt this would give a file with just 2 lines, i.e. echo 'helo'>out.txt n helo
      – mandrake00
      Nov 10 at 14:45




      1




      1




      Do you want this to happen for every single command you ever type in any terminal? Just for specific commands? Please edit your question and give us more specific requirements.
      – terdon
      Nov 10 at 14:46




      Do you want this to happen for every single command you ever type in any terminal? Just for specific commands? Please edit your question and give us more specific requirements.
      – terdon
      Nov 10 at 14:46




      4




      4




      You can suppress the lines about when it was started and stopped by using script -q
      – Panki
      Nov 10 at 14:56




      You can suppress the lines about when it was started and stopped by using script -q
      – Panki
      Nov 10 at 14:56












      up vote
      5
      down vote













      In the genearality you're asking for, this is not possible.



      To achieve this with additional tools (akin to script(1)), it would be necessary for that program, observing your shell, that in



      command >foo


      foo is the name of a file, and create it. Also, the shell would try to create it, so there's conflict already. Also, what if the program would print



      command >foo


      to the terminal?



      From inside the shell, well, the shell's redirection process is extremely primitive from a programming point of view (try to find a C lecture where they build a primitive shell themselves, it is really surprising).



      If you really need this, you would have to hack your own shell to intercept redirections and pass the command into the target file. But there are probably a lot of nasty issues in the details...






      share|improve this answer
























        up vote
        5
        down vote













        In the genearality you're asking for, this is not possible.



        To achieve this with additional tools (akin to script(1)), it would be necessary for that program, observing your shell, that in



        command >foo


        foo is the name of a file, and create it. Also, the shell would try to create it, so there's conflict already. Also, what if the program would print



        command >foo


        to the terminal?



        From inside the shell, well, the shell's redirection process is extremely primitive from a programming point of view (try to find a C lecture where they build a primitive shell themselves, it is really surprising).



        If you really need this, you would have to hack your own shell to intercept redirections and pass the command into the target file. But there are probably a lot of nasty issues in the details...






        share|improve this answer






















          up vote
          5
          down vote










          up vote
          5
          down vote









          In the genearality you're asking for, this is not possible.



          To achieve this with additional tools (akin to script(1)), it would be necessary for that program, observing your shell, that in



          command >foo


          foo is the name of a file, and create it. Also, the shell would try to create it, so there's conflict already. Also, what if the program would print



          command >foo


          to the terminal?



          From inside the shell, well, the shell's redirection process is extremely primitive from a programming point of view (try to find a C lecture where they build a primitive shell themselves, it is really surprising).



          If you really need this, you would have to hack your own shell to intercept redirections and pass the command into the target file. But there are probably a lot of nasty issues in the details...






          share|improve this answer












          In the genearality you're asking for, this is not possible.



          To achieve this with additional tools (akin to script(1)), it would be necessary for that program, observing your shell, that in



          command >foo


          foo is the name of a file, and create it. Also, the shell would try to create it, so there's conflict already. Also, what if the program would print



          command >foo


          to the terminal?



          From inside the shell, well, the shell's redirection process is extremely primitive from a programming point of view (try to find a C lecture where they build a primitive shell themselves, it is really surprising).



          If you really need this, you would have to hack your own shell to intercept redirections and pass the command into the target file. But there are probably a lot of nasty issues in the details...







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 16:16









          stefan

          560312




          560312




















              up vote
              4
              down vote













              You can also pipe the whole shell:



              $ sh -i |& tee sh.log
              sh-4.4$ hello
              sh: hello: command not found
              sh-4.4$ echo hi
              hi
              sh-4.4$ exit


              -i is needed to keep the shell interactive despite stdout not being a terminal. bash and zsh also support that option. |& pipes stdout and stderr; it works with zsh and bash, but not sh (there, you'd need 2>&1 |). Of course, you could also use &> or 2>&1 > if you just want to redirect to a file and nothing more. Anyways, sh.log here contains everything.



              $ cat sh.log
              sh-4.4$ hello
              sh: hello: command not found
              sh-4.4$ echo hi
              hi
              sh-4.4$ exit





              share|improve this answer


























                up vote
                4
                down vote













                You can also pipe the whole shell:



                $ sh -i |& tee sh.log
                sh-4.4$ hello
                sh: hello: command not found
                sh-4.4$ echo hi
                hi
                sh-4.4$ exit


                -i is needed to keep the shell interactive despite stdout not being a terminal. bash and zsh also support that option. |& pipes stdout and stderr; it works with zsh and bash, but not sh (there, you'd need 2>&1 |). Of course, you could also use &> or 2>&1 > if you just want to redirect to a file and nothing more. Anyways, sh.log here contains everything.



                $ cat sh.log
                sh-4.4$ hello
                sh: hello: command not found
                sh-4.4$ echo hi
                hi
                sh-4.4$ exit





                share|improve this answer
























                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  You can also pipe the whole shell:



                  $ sh -i |& tee sh.log
                  sh-4.4$ hello
                  sh: hello: command not found
                  sh-4.4$ echo hi
                  hi
                  sh-4.4$ exit


                  -i is needed to keep the shell interactive despite stdout not being a terminal. bash and zsh also support that option. |& pipes stdout and stderr; it works with zsh and bash, but not sh (there, you'd need 2>&1 |). Of course, you could also use &> or 2>&1 > if you just want to redirect to a file and nothing more. Anyways, sh.log here contains everything.



                  $ cat sh.log
                  sh-4.4$ hello
                  sh: hello: command not found
                  sh-4.4$ echo hi
                  hi
                  sh-4.4$ exit





                  share|improve this answer














                  You can also pipe the whole shell:



                  $ sh -i |& tee sh.log
                  sh-4.4$ hello
                  sh: hello: command not found
                  sh-4.4$ echo hi
                  hi
                  sh-4.4$ exit


                  -i is needed to keep the shell interactive despite stdout not being a terminal. bash and zsh also support that option. |& pipes stdout and stderr; it works with zsh and bash, but not sh (there, you'd need 2>&1 |). Of course, you could also use &> or 2>&1 > if you just want to redirect to a file and nothing more. Anyways, sh.log here contains everything.



                  $ cat sh.log
                  sh-4.4$ hello
                  sh: hello: command not found
                  sh-4.4$ echo hi
                  hi
                  sh-4.4$ exit






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 10 at 21:45

























                  answered Nov 10 at 16:20









                  JoL

                  80839




                  80839




















                      up vote
                      2
                      down vote













                      If it's acceptable for you to drop the output filename, I think the following is the easiest way:



                      command='echo "hello!"'
                      (echo "$command"; eval "$command") >output.txt


                      But if you need to print the filename then you could use the following:



                      command='echo "hello!"'
                      (echo "$command >output.txt"; eval "$command") >output.txt


                      You can introduce a variable for the filename in order to make sure that it is the same in the two occurrences:



                      output="output.txt"
                      command='echo "hello!"'
                      (echo "$command >$output"; eval "$command") >$output


                      This way you avoid the risk of having a difference in the printed filename and the actual filename.






                      share|improve this answer


















                      • 1




                        Generalizing as a function: f() command="$1"; file="$2"; (printf '%s >%s' "$command" "$file"; eval "$command") >"$file"; Then call: f 'echo "Hello!"' output.txt
                        – wjandrea
                        Nov 10 at 19:20














                      up vote
                      2
                      down vote













                      If it's acceptable for you to drop the output filename, I think the following is the easiest way:



                      command='echo "hello!"'
                      (echo "$command"; eval "$command") >output.txt


                      But if you need to print the filename then you could use the following:



                      command='echo "hello!"'
                      (echo "$command >output.txt"; eval "$command") >output.txt


                      You can introduce a variable for the filename in order to make sure that it is the same in the two occurrences:



                      output="output.txt"
                      command='echo "hello!"'
                      (echo "$command >$output"; eval "$command") >$output


                      This way you avoid the risk of having a difference in the printed filename and the actual filename.






                      share|improve this answer


















                      • 1




                        Generalizing as a function: f() command="$1"; file="$2"; (printf '%s >%s' "$command" "$file"; eval "$command") >"$file"; Then call: f 'echo "Hello!"' output.txt
                        – wjandrea
                        Nov 10 at 19:20












                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      If it's acceptable for you to drop the output filename, I think the following is the easiest way:



                      command='echo "hello!"'
                      (echo "$command"; eval "$command") >output.txt


                      But if you need to print the filename then you could use the following:



                      command='echo "hello!"'
                      (echo "$command >output.txt"; eval "$command") >output.txt


                      You can introduce a variable for the filename in order to make sure that it is the same in the two occurrences:



                      output="output.txt"
                      command='echo "hello!"'
                      (echo "$command >$output"; eval "$command") >$output


                      This way you avoid the risk of having a difference in the printed filename and the actual filename.






                      share|improve this answer














                      If it's acceptable for you to drop the output filename, I think the following is the easiest way:



                      command='echo "hello!"'
                      (echo "$command"; eval "$command") >output.txt


                      But if you need to print the filename then you could use the following:



                      command='echo "hello!"'
                      (echo "$command >output.txt"; eval "$command") >output.txt


                      You can introduce a variable for the filename in order to make sure that it is the same in the two occurrences:



                      output="output.txt"
                      command='echo "hello!"'
                      (echo "$command >$output"; eval "$command") >$output


                      This way you avoid the risk of having a difference in the printed filename and the actual filename.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 10 at 20:04









                      wjandrea

                      462413




                      462413










                      answered Nov 10 at 15:25









                      Erwan

                      1664




                      1664







                      • 1




                        Generalizing as a function: f() command="$1"; file="$2"; (printf '%s >%s' "$command" "$file"; eval "$command") >"$file"; Then call: f 'echo "Hello!"' output.txt
                        – wjandrea
                        Nov 10 at 19:20












                      • 1




                        Generalizing as a function: f() command="$1"; file="$2"; (printf '%s >%s' "$command" "$file"; eval "$command") >"$file"; Then call: f 'echo "Hello!"' output.txt
                        – wjandrea
                        Nov 10 at 19:20







                      1




                      1




                      Generalizing as a function: f() command="$1"; file="$2"; (printf '%s >%s' "$command" "$file"; eval "$command") >"$file"; Then call: f 'echo "Hello!"' output.txt
                      – wjandrea
                      Nov 10 at 19:20




                      Generalizing as a function: f() command="$1"; file="$2"; (printf '%s >%s' "$command" "$file"; eval "$command") >"$file"; Then call: f 'echo "Hello!"' output.txt
                      – wjandrea
                      Nov 10 at 19:20

















                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480956%2fredirect-to-file-including-current-prompt-line%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

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線