How to send multiple commands to sftp using one line












12















The following command sends one command to sftp using one line:



sftp -o PasswordAuthentication=no user@host" <<<"lcd /home"


How to send multiple lines to sftp using one line. Is there a way to insert carriage returns or something to achieve this, for example:



sftp -o PasswordAuthentication=no user@host" <<<"lcd /homen cd /myhomen get file"


The idea is to NOT use the sftp -b option where an external file listing commands is loaded.










share|improve this question





























    12















    The following command sends one command to sftp using one line:



    sftp -o PasswordAuthentication=no user@host" <<<"lcd /home"


    How to send multiple lines to sftp using one line. Is there a way to insert carriage returns or something to achieve this, for example:



    sftp -o PasswordAuthentication=no user@host" <<<"lcd /homen cd /myhomen get file"


    The idea is to NOT use the sftp -b option where an external file listing commands is loaded.










    share|improve this question



























      12












      12








      12


      3






      The following command sends one command to sftp using one line:



      sftp -o PasswordAuthentication=no user@host" <<<"lcd /home"


      How to send multiple lines to sftp using one line. Is there a way to insert carriage returns or something to achieve this, for example:



      sftp -o PasswordAuthentication=no user@host" <<<"lcd /homen cd /myhomen get file"


      The idea is to NOT use the sftp -b option where an external file listing commands is loaded.










      share|improve this question
















      The following command sends one command to sftp using one line:



      sftp -o PasswordAuthentication=no user@host" <<<"lcd /home"


      How to send multiple lines to sftp using one line. Is there a way to insert carriage returns or something to achieve this, for example:



      sftp -o PasswordAuthentication=no user@host" <<<"lcd /homen cd /myhomen get file"


      The idea is to NOT use the sftp -b option where an external file listing commands is loaded.







      scripting sftp






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 '12 at 23:09









      Gilles

      531k12810631591




      531k12810631591










      asked Nov 8 '12 at 13:51









      Radnaskela SamotRadnaskela Samot

      71118




      71118






















          6 Answers
          6






          active

          oldest

          votes


















          11














          From the here-string (<<<) syntax you used I guess your shell is bash, so you can also use string with backslash-escaped characters ($''):



          sftp -o PasswordAuthentication=no user@host <<< $'lcd /homen cd /myhomen get file'


          The portable alternative is here-document:



          sftp -o PasswordAuthentication=no user@host <<END
          lcd /home
          cd /myhome
          get file
          END





          share|improve this answer



















          • 1





            <<< was introduced first by zsh (though inspired by rc) and was later added to ksh93 and then bash.

            – Stéphane Chazelas
            Nov 8 '12 at 22:34











          • Thank you, @StephaneChazelas. I really need to strengthen my zsh skills.

            – manatwork
            Nov 9 '12 at 8:49



















          7














          Use the -b/--batchfile option to have proper error handling:



           printf '%sn' 'lcd /home' 'cd /myhome' 'get file' | sftp -b - user@host





          share|improve this answer



















          • 1





            To use variables change ' to "

            – Radnaskela Samot
            Nov 9 '12 at 8:43





















          2














          Yes, you can just use echo -e



          echo -e "lcd /homencd /myhomenget file" | sftp user@host





          share|improve this answer



















          • 3





            echo is very non-standard - echo -e doesn't work the same everywhere, so you'd have to check first. Alternatively, just use printf which is much more portable and should work the same everywhere.

            – jw013
            Nov 8 '12 at 16:59



















          1














          It is not necessary to avoid the -b option to avoid writing the batch file to disk. Using process substitution you can create the batch on the fly.



          batch() {
          echo lcd /home
          echo cd /myhome
          echo get file
          }

          sftp -b <(batch) -o PasswordAuthentication=no user@host





          share|improve this answer































            0














            Use native sftp command




            sftp -o PasswordAuthentication=no user@host:/home/myhome/file







            share|improve this answer
























            • What you didn't notice was that lcd and cd do different things. You've just combined the paths into something that will not exist.

              – underscore_d
              Aug 14 '18 at 13:14



















            0














            Mybru, you can mos pipe your commands like so:



            echo '
            lcd /home
            cd /myhome
            get file
            ' | sftp -o PasswordAuthentication=no user@host





            share|improve this answer























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "106"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f55190%2fhow-to-send-multiple-commands-to-sftp-using-one-line%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              11














              From the here-string (<<<) syntax you used I guess your shell is bash, so you can also use string with backslash-escaped characters ($''):



              sftp -o PasswordAuthentication=no user@host <<< $'lcd /homen cd /myhomen get file'


              The portable alternative is here-document:



              sftp -o PasswordAuthentication=no user@host <<END
              lcd /home
              cd /myhome
              get file
              END





              share|improve this answer



















              • 1





                <<< was introduced first by zsh (though inspired by rc) and was later added to ksh93 and then bash.

                – Stéphane Chazelas
                Nov 8 '12 at 22:34











              • Thank you, @StephaneChazelas. I really need to strengthen my zsh skills.

                – manatwork
                Nov 9 '12 at 8:49
















              11














              From the here-string (<<<) syntax you used I guess your shell is bash, so you can also use string with backslash-escaped characters ($''):



              sftp -o PasswordAuthentication=no user@host <<< $'lcd /homen cd /myhomen get file'


              The portable alternative is here-document:



              sftp -o PasswordAuthentication=no user@host <<END
              lcd /home
              cd /myhome
              get file
              END





              share|improve this answer



















              • 1





                <<< was introduced first by zsh (though inspired by rc) and was later added to ksh93 and then bash.

                – Stéphane Chazelas
                Nov 8 '12 at 22:34











              • Thank you, @StephaneChazelas. I really need to strengthen my zsh skills.

                – manatwork
                Nov 9 '12 at 8:49














              11












              11








              11







              From the here-string (<<<) syntax you used I guess your shell is bash, so you can also use string with backslash-escaped characters ($''):



              sftp -o PasswordAuthentication=no user@host <<< $'lcd /homen cd /myhomen get file'


              The portable alternative is here-document:



              sftp -o PasswordAuthentication=no user@host <<END
              lcd /home
              cd /myhome
              get file
              END





              share|improve this answer













              From the here-string (<<<) syntax you used I guess your shell is bash, so you can also use string with backslash-escaped characters ($''):



              sftp -o PasswordAuthentication=no user@host <<< $'lcd /homen cd /myhomen get file'


              The portable alternative is here-document:



              sftp -o PasswordAuthentication=no user@host <<END
              lcd /home
              cd /myhome
              get file
              END






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 8 '12 at 14:51









              manatworkmanatwork

              21.7k38384




              21.7k38384








              • 1





                <<< was introduced first by zsh (though inspired by rc) and was later added to ksh93 and then bash.

                – Stéphane Chazelas
                Nov 8 '12 at 22:34











              • Thank you, @StephaneChazelas. I really need to strengthen my zsh skills.

                – manatwork
                Nov 9 '12 at 8:49














              • 1





                <<< was introduced first by zsh (though inspired by rc) and was later added to ksh93 and then bash.

                – Stéphane Chazelas
                Nov 8 '12 at 22:34











              • Thank you, @StephaneChazelas. I really need to strengthen my zsh skills.

                – manatwork
                Nov 9 '12 at 8:49








              1




              1





              <<< was introduced first by zsh (though inspired by rc) and was later added to ksh93 and then bash.

              – Stéphane Chazelas
              Nov 8 '12 at 22:34





              <<< was introduced first by zsh (though inspired by rc) and was later added to ksh93 and then bash.

              – Stéphane Chazelas
              Nov 8 '12 at 22:34













              Thank you, @StephaneChazelas. I really need to strengthen my zsh skills.

              – manatwork
              Nov 9 '12 at 8:49





              Thank you, @StephaneChazelas. I really need to strengthen my zsh skills.

              – manatwork
              Nov 9 '12 at 8:49













              7














              Use the -b/--batchfile option to have proper error handling:



               printf '%sn' 'lcd /home' 'cd /myhome' 'get file' | sftp -b - user@host





              share|improve this answer



















              • 1





                To use variables change ' to "

                – Radnaskela Samot
                Nov 9 '12 at 8:43


















              7














              Use the -b/--batchfile option to have proper error handling:



               printf '%sn' 'lcd /home' 'cd /myhome' 'get file' | sftp -b - user@host





              share|improve this answer



















              • 1





                To use variables change ' to "

                – Radnaskela Samot
                Nov 9 '12 at 8:43
















              7












              7








              7







              Use the -b/--batchfile option to have proper error handling:



               printf '%sn' 'lcd /home' 'cd /myhome' 'get file' | sftp -b - user@host





              share|improve this answer













              Use the -b/--batchfile option to have proper error handling:



               printf '%sn' 'lcd /home' 'cd /myhome' 'get file' | sftp -b - user@host






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 8 '12 at 22:33









              Stéphane ChazelasStéphane Chazelas

              301k55564916




              301k55564916








              • 1





                To use variables change ' to "

                – Radnaskela Samot
                Nov 9 '12 at 8:43
















              • 1





                To use variables change ' to "

                – Radnaskela Samot
                Nov 9 '12 at 8:43










              1




              1





              To use variables change ' to "

              – Radnaskela Samot
              Nov 9 '12 at 8:43







              To use variables change ' to "

              – Radnaskela Samot
              Nov 9 '12 at 8:43













              2














              Yes, you can just use echo -e



              echo -e "lcd /homencd /myhomenget file" | sftp user@host





              share|improve this answer



















              • 3





                echo is very non-standard - echo -e doesn't work the same everywhere, so you'd have to check first. Alternatively, just use printf which is much more portable and should work the same everywhere.

                – jw013
                Nov 8 '12 at 16:59
















              2














              Yes, you can just use echo -e



              echo -e "lcd /homencd /myhomenget file" | sftp user@host





              share|improve this answer



















              • 3





                echo is very non-standard - echo -e doesn't work the same everywhere, so you'd have to check first. Alternatively, just use printf which is much more portable and should work the same everywhere.

                – jw013
                Nov 8 '12 at 16:59














              2












              2








              2







              Yes, you can just use echo -e



              echo -e "lcd /homencd /myhomenget file" | sftp user@host





              share|improve this answer













              Yes, you can just use echo -e



              echo -e "lcd /homencd /myhomenget file" | sftp user@host






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 8 '12 at 14:43









              utopiaboundutopiabound

              2,6611518




              2,6611518








              • 3





                echo is very non-standard - echo -e doesn't work the same everywhere, so you'd have to check first. Alternatively, just use printf which is much more portable and should work the same everywhere.

                – jw013
                Nov 8 '12 at 16:59














              • 3





                echo is very non-standard - echo -e doesn't work the same everywhere, so you'd have to check first. Alternatively, just use printf which is much more portable and should work the same everywhere.

                – jw013
                Nov 8 '12 at 16:59








              3




              3





              echo is very non-standard - echo -e doesn't work the same everywhere, so you'd have to check first. Alternatively, just use printf which is much more portable and should work the same everywhere.

              – jw013
              Nov 8 '12 at 16:59





              echo is very non-standard - echo -e doesn't work the same everywhere, so you'd have to check first. Alternatively, just use printf which is much more portable and should work the same everywhere.

              – jw013
              Nov 8 '12 at 16:59











              1














              It is not necessary to avoid the -b option to avoid writing the batch file to disk. Using process substitution you can create the batch on the fly.



              batch() {
              echo lcd /home
              echo cd /myhome
              echo get file
              }

              sftp -b <(batch) -o PasswordAuthentication=no user@host





              share|improve this answer




























                1














                It is not necessary to avoid the -b option to avoid writing the batch file to disk. Using process substitution you can create the batch on the fly.



                batch() {
                echo lcd /home
                echo cd /myhome
                echo get file
                }

                sftp -b <(batch) -o PasswordAuthentication=no user@host





                share|improve this answer


























                  1












                  1








                  1







                  It is not necessary to avoid the -b option to avoid writing the batch file to disk. Using process substitution you can create the batch on the fly.



                  batch() {
                  echo lcd /home
                  echo cd /myhome
                  echo get file
                  }

                  sftp -b <(batch) -o PasswordAuthentication=no user@host





                  share|improve this answer













                  It is not necessary to avoid the -b option to avoid writing the batch file to disk. Using process substitution you can create the batch on the fly.



                  batch() {
                  echo lcd /home
                  echo cd /myhome
                  echo get file
                  }

                  sftp -b <(batch) -o PasswordAuthentication=no user@host






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 18 '17 at 10:38









                  cevingceving

                  1,70421321




                  1,70421321























                      0














                      Use native sftp command




                      sftp -o PasswordAuthentication=no user@host:/home/myhome/file







                      share|improve this answer
























                      • What you didn't notice was that lcd and cd do different things. You've just combined the paths into something that will not exist.

                        – underscore_d
                        Aug 14 '18 at 13:14
















                      0














                      Use native sftp command




                      sftp -o PasswordAuthentication=no user@host:/home/myhome/file







                      share|improve this answer
























                      • What you didn't notice was that lcd and cd do different things. You've just combined the paths into something that will not exist.

                        – underscore_d
                        Aug 14 '18 at 13:14














                      0












                      0








                      0







                      Use native sftp command




                      sftp -o PasswordAuthentication=no user@host:/home/myhome/file







                      share|improve this answer













                      Use native sftp command




                      sftp -o PasswordAuthentication=no user@host:/home/myhome/file








                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 19 '13 at 0:55









                      MarkomafsMarkomafs

                      1013




                      1013













                      • What you didn't notice was that lcd and cd do different things. You've just combined the paths into something that will not exist.

                        – underscore_d
                        Aug 14 '18 at 13:14



















                      • What you didn't notice was that lcd and cd do different things. You've just combined the paths into something that will not exist.

                        – underscore_d
                        Aug 14 '18 at 13:14

















                      What you didn't notice was that lcd and cd do different things. You've just combined the paths into something that will not exist.

                      – underscore_d
                      Aug 14 '18 at 13:14





                      What you didn't notice was that lcd and cd do different things. You've just combined the paths into something that will not exist.

                      – underscore_d
                      Aug 14 '18 at 13:14











                      0














                      Mybru, you can mos pipe your commands like so:



                      echo '
                      lcd /home
                      cd /myhome
                      get file
                      ' | sftp -o PasswordAuthentication=no user@host





                      share|improve this answer




























                        0














                        Mybru, you can mos pipe your commands like so:



                        echo '
                        lcd /home
                        cd /myhome
                        get file
                        ' | sftp -o PasswordAuthentication=no user@host





                        share|improve this answer


























                          0












                          0








                          0







                          Mybru, you can mos pipe your commands like so:



                          echo '
                          lcd /home
                          cd /myhome
                          get file
                          ' | sftp -o PasswordAuthentication=no user@host





                          share|improve this answer













                          Mybru, you can mos pipe your commands like so:



                          echo '
                          lcd /home
                          cd /myhome
                          get file
                          ' | sftp -o PasswordAuthentication=no user@host






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 4 at 11:49









                          Paque MannPaque Mann

                          1




                          1






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f55190%2fhow-to-send-multiple-commands-to-sftp-using-one-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

                              Morgemoulin

                              Scott Moir

                              Souastre