writing a command in a file

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











up vote
1
down vote

favorite












I'm having a difficulty writing a command in a file using a bash script (NOT the output of the command + I don't want the command to be executed)
I want the file to simply have run $(python -c "print('A'*256)") inside of it.



I've tried adding it in a variable then passing it to a file by echo, that didn't work.



I also tried something like echo "run $(python -c "print('A'*256)")" > file.txt



Also didn't work, I actually got some errors so I assumed that the problem is because of the multiple double quotations, so I changed them to single quotations. That gave me this output in the file run



So it basically ignored $(python -c "print('A'*256)") completely.



Any idea how to resolve this?







share|improve this question
























    up vote
    1
    down vote

    favorite












    I'm having a difficulty writing a command in a file using a bash script (NOT the output of the command + I don't want the command to be executed)
    I want the file to simply have run $(python -c "print('A'*256)") inside of it.



    I've tried adding it in a variable then passing it to a file by echo, that didn't work.



    I also tried something like echo "run $(python -c "print('A'*256)")" > file.txt



    Also didn't work, I actually got some errors so I assumed that the problem is because of the multiple double quotations, so I changed them to single quotations. That gave me this output in the file run



    So it basically ignored $(python -c "print('A'*256)") completely.



    Any idea how to resolve this?







    share|improve this question






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm having a difficulty writing a command in a file using a bash script (NOT the output of the command + I don't want the command to be executed)
      I want the file to simply have run $(python -c "print('A'*256)") inside of it.



      I've tried adding it in a variable then passing it to a file by echo, that didn't work.



      I also tried something like echo "run $(python -c "print('A'*256)")" > file.txt



      Also didn't work, I actually got some errors so I assumed that the problem is because of the multiple double quotations, so I changed them to single quotations. That gave me this output in the file run



      So it basically ignored $(python -c "print('A'*256)") completely.



      Any idea how to resolve this?







      share|improve this question












      I'm having a difficulty writing a command in a file using a bash script (NOT the output of the command + I don't want the command to be executed)
      I want the file to simply have run $(python -c "print('A'*256)") inside of it.



      I've tried adding it in a variable then passing it to a file by echo, that didn't work.



      I also tried something like echo "run $(python -c "print('A'*256)")" > file.txt



      Also didn't work, I actually got some errors so I assumed that the problem is because of the multiple double quotations, so I changed them to single quotations. That gave me this output in the file run



      So it basically ignored $(python -c "print('A'*256)") completely.



      Any idea how to resolve this?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 4 '17 at 22:19









      Jack

      61




      61




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote













          The most reliable way to get arbitrary strings into redirections is to use a here document:



          cat > file.txt <<'EOT'
          run $(python -c "print('A'*256)")
          EOT


          This will put that single line exactly as-is into file.txt. The text up until EOT is given to cat as its standard input, and cat just spits it out again to be redirected. The shell will keep reading the document until EOT appears at the start of the line, so you could put multiple lines in at once if you wanted (if EOT appears like that within your string, use something else instead - the delimiter is arbitrary).



          Because the delimiter is quoted at the top, no expansions are performed on the body of the here-document1 and all your characters are passed through untouched. The behaviour of here-documents is specified by POSIX and will work in any shell.



          1There was a bug in some older versions of Bash that did perform some expansions in certain cases, but you won't hit that here.






          share|improve this answer



























            up vote
            0
            down vote













            Try to use escape characters instead:



            echo run $(python -c "print('A'*256)") > file.txt






            share|improve this answer




















            • IMHO, this is ugly and hard to get right (easy to get wrong).   But it does work, unlike the other answer.
              – G-Man
              Nov 4 '17 at 23:30

















            up vote
            0
            down vote













            A string with *both kinds of quotes in it is always hard to handle. 
            There are three approaches:




            • Alternate between the two kinds of quotes. 
              Put the double quotes (") inside single quotes (') and vice versa.



              First break your string into three pieces:



              run $(python -c "print ('A'*256) ")
              ↑---------1----------↑ ↑---2---↑ ↑3↑


              where the first and third part contain no single quotes,
              and the second part has no double quotes. 
              Then quote them appropriately:



              echo 'run $(python -c "print'"('A'*256)"'")'
              ↑----------1-----------↑↑----2----↑↑-3↑


              just running the substrings together.




            • Quote the string with double quotes,
              and then use " for every literal double quote
              that you want to have echoed. 
              Also replace $ with $



              echo "run $(python -c "print('A'*256)")"



            • An approach that works in bash only is to quote the string with $'…'. 
              Inside such a string, you can escape single quotes with ':



              echo $'run $(python -c "print('A'*256)")'


            Of course the > file.txt part is as you would expect.






            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',
              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%2f402559%2fwriting-a-command-in-a-file%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
              3
              down vote













              The most reliable way to get arbitrary strings into redirections is to use a here document:



              cat > file.txt <<'EOT'
              run $(python -c "print('A'*256)")
              EOT


              This will put that single line exactly as-is into file.txt. The text up until EOT is given to cat as its standard input, and cat just spits it out again to be redirected. The shell will keep reading the document until EOT appears at the start of the line, so you could put multiple lines in at once if you wanted (if EOT appears like that within your string, use something else instead - the delimiter is arbitrary).



              Because the delimiter is quoted at the top, no expansions are performed on the body of the here-document1 and all your characters are passed through untouched. The behaviour of here-documents is specified by POSIX and will work in any shell.



              1There was a bug in some older versions of Bash that did perform some expansions in certain cases, but you won't hit that here.






              share|improve this answer
























                up vote
                3
                down vote













                The most reliable way to get arbitrary strings into redirections is to use a here document:



                cat > file.txt <<'EOT'
                run $(python -c "print('A'*256)")
                EOT


                This will put that single line exactly as-is into file.txt. The text up until EOT is given to cat as its standard input, and cat just spits it out again to be redirected. The shell will keep reading the document until EOT appears at the start of the line, so you could put multiple lines in at once if you wanted (if EOT appears like that within your string, use something else instead - the delimiter is arbitrary).



                Because the delimiter is quoted at the top, no expansions are performed on the body of the here-document1 and all your characters are passed through untouched. The behaviour of here-documents is specified by POSIX and will work in any shell.



                1There was a bug in some older versions of Bash that did perform some expansions in certain cases, but you won't hit that here.






                share|improve this answer






















                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  The most reliable way to get arbitrary strings into redirections is to use a here document:



                  cat > file.txt <<'EOT'
                  run $(python -c "print('A'*256)")
                  EOT


                  This will put that single line exactly as-is into file.txt. The text up until EOT is given to cat as its standard input, and cat just spits it out again to be redirected. The shell will keep reading the document until EOT appears at the start of the line, so you could put multiple lines in at once if you wanted (if EOT appears like that within your string, use something else instead - the delimiter is arbitrary).



                  Because the delimiter is quoted at the top, no expansions are performed on the body of the here-document1 and all your characters are passed through untouched. The behaviour of here-documents is specified by POSIX and will work in any shell.



                  1There was a bug in some older versions of Bash that did perform some expansions in certain cases, but you won't hit that here.






                  share|improve this answer












                  The most reliable way to get arbitrary strings into redirections is to use a here document:



                  cat > file.txt <<'EOT'
                  run $(python -c "print('A'*256)")
                  EOT


                  This will put that single line exactly as-is into file.txt. The text up until EOT is given to cat as its standard input, and cat just spits it out again to be redirected. The shell will keep reading the document until EOT appears at the start of the line, so you could put multiple lines in at once if you wanted (if EOT appears like that within your string, use something else instead - the delimiter is arbitrary).



                  Because the delimiter is quoted at the top, no expansions are performed on the body of the here-document1 and all your characters are passed through untouched. The behaviour of here-documents is specified by POSIX and will work in any shell.



                  1There was a bug in some older versions of Bash that did perform some expansions in certain cases, but you won't hit that here.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 5 '17 at 0:30









                  Michael Homer

                  42.6k6108148




                  42.6k6108148






















                      up vote
                      0
                      down vote













                      Try to use escape characters instead:



                      echo run $(python -c "print('A'*256)") > file.txt






                      share|improve this answer




















                      • IMHO, this is ugly and hard to get right (easy to get wrong).   But it does work, unlike the other answer.
                        – G-Man
                        Nov 4 '17 at 23:30














                      up vote
                      0
                      down vote













                      Try to use escape characters instead:



                      echo run $(python -c "print('A'*256)") > file.txt






                      share|improve this answer




















                      • IMHO, this is ugly and hard to get right (easy to get wrong).   But it does work, unlike the other answer.
                        – G-Man
                        Nov 4 '17 at 23:30












                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      Try to use escape characters instead:



                      echo run $(python -c "print('A'*256)") > file.txt






                      share|improve this answer












                      Try to use escape characters instead:



                      echo run $(python -c "print('A'*256)") > file.txt







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 4 '17 at 23:17









                      Śubham

                      844




                      844











                      • IMHO, this is ugly and hard to get right (easy to get wrong).   But it does work, unlike the other answer.
                        – G-Man
                        Nov 4 '17 at 23:30
















                      • IMHO, this is ugly and hard to get right (easy to get wrong).   But it does work, unlike the other answer.
                        – G-Man
                        Nov 4 '17 at 23:30















                      IMHO, this is ugly and hard to get right (easy to get wrong).   But it does work, unlike the other answer.
                      – G-Man
                      Nov 4 '17 at 23:30




                      IMHO, this is ugly and hard to get right (easy to get wrong).   But it does work, unlike the other answer.
                      – G-Man
                      Nov 4 '17 at 23:30










                      up vote
                      0
                      down vote













                      A string with *both kinds of quotes in it is always hard to handle. 
                      There are three approaches:




                      • Alternate between the two kinds of quotes. 
                        Put the double quotes (") inside single quotes (') and vice versa.



                        First break your string into three pieces:



                        run $(python -c "print ('A'*256) ")
                        ↑---------1----------↑ ↑---2---↑ ↑3↑


                        where the first and third part contain no single quotes,
                        and the second part has no double quotes. 
                        Then quote them appropriately:



                        echo 'run $(python -c "print'"('A'*256)"'")'
                        ↑----------1-----------↑↑----2----↑↑-3↑


                        just running the substrings together.




                      • Quote the string with double quotes,
                        and then use " for every literal double quote
                        that you want to have echoed. 
                        Also replace $ with $



                        echo "run $(python -c "print('A'*256)")"



                      • An approach that works in bash only is to quote the string with $'…'. 
                        Inside such a string, you can escape single quotes with ':



                        echo $'run $(python -c "print('A'*256)")'


                      Of course the > file.txt part is as you would expect.






                      share|improve this answer


























                        up vote
                        0
                        down vote













                        A string with *both kinds of quotes in it is always hard to handle. 
                        There are three approaches:




                        • Alternate between the two kinds of quotes. 
                          Put the double quotes (") inside single quotes (') and vice versa.



                          First break your string into three pieces:



                          run $(python -c "print ('A'*256) ")
                          ↑---------1----------↑ ↑---2---↑ ↑3↑


                          where the first and third part contain no single quotes,
                          and the second part has no double quotes. 
                          Then quote them appropriately:



                          echo 'run $(python -c "print'"('A'*256)"'")'
                          ↑----------1-----------↑↑----2----↑↑-3↑


                          just running the substrings together.




                        • Quote the string with double quotes,
                          and then use " for every literal double quote
                          that you want to have echoed. 
                          Also replace $ with $



                          echo "run $(python -c "print('A'*256)")"



                        • An approach that works in bash only is to quote the string with $'…'. 
                          Inside such a string, you can escape single quotes with ':



                          echo $'run $(python -c "print('A'*256)")'


                        Of course the > file.txt part is as you would expect.






                        share|improve this answer
























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          A string with *both kinds of quotes in it is always hard to handle. 
                          There are three approaches:




                          • Alternate between the two kinds of quotes. 
                            Put the double quotes (") inside single quotes (') and vice versa.



                            First break your string into three pieces:



                            run $(python -c "print ('A'*256) ")
                            ↑---------1----------↑ ↑---2---↑ ↑3↑


                            where the first and third part contain no single quotes,
                            and the second part has no double quotes. 
                            Then quote them appropriately:



                            echo 'run $(python -c "print'"('A'*256)"'")'
                            ↑----------1-----------↑↑----2----↑↑-3↑


                            just running the substrings together.




                          • Quote the string with double quotes,
                            and then use " for every literal double quote
                            that you want to have echoed. 
                            Also replace $ with $



                            echo "run $(python -c "print('A'*256)")"



                          • An approach that works in bash only is to quote the string with $'…'. 
                            Inside such a string, you can escape single quotes with ':



                            echo $'run $(python -c "print('A'*256)")'


                          Of course the > file.txt part is as you would expect.






                          share|improve this answer














                          A string with *both kinds of quotes in it is always hard to handle. 
                          There are three approaches:




                          • Alternate between the two kinds of quotes. 
                            Put the double quotes (") inside single quotes (') and vice versa.



                            First break your string into three pieces:



                            run $(python -c "print ('A'*256) ")
                            ↑---------1----------↑ ↑---2---↑ ↑3↑


                            where the first and third part contain no single quotes,
                            and the second part has no double quotes. 
                            Then quote them appropriately:



                            echo 'run $(python -c "print'"('A'*256)"'")'
                            ↑----------1-----------↑↑----2----↑↑-3↑


                            just running the substrings together.




                          • Quote the string with double quotes,
                            and then use " for every literal double quote
                            that you want to have echoed. 
                            Also replace $ with $



                            echo "run $(python -c "print('A'*256)")"



                          • An approach that works in bash only is to quote the string with $'…'. 
                            Inside such a string, you can escape single quotes with ':



                            echo $'run $(python -c "print('A'*256)")'


                          Of course the > file.txt part is as you would expect.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 4 '17 at 23:19

























                          answered Nov 4 '17 at 23:08









                          G-Man

                          11.6k82657




                          11.6k82657



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402559%2fwriting-a-command-in-a-file%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?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay