How to send multiple commands to sftp using one line

scriptingsftp

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 /home\n cd /myhome\n get file"

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

Best 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 /home\n cd /myhome\n get file'

The portable alternative is here-document:

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