admin管理员组

文章数量:1192529

When I run the script test.sh below from an MSYS2 terminal on Windows the newlines are not honored in the ssh command. The remote shell is cmd and the file is saved with Windows line endings (CR LF). Any ideas?

$ cat test.sh
text=$(cat <<'EOF'
hello
there
EOF
) 
printf "%s\n\n" "$text"

ssh A-WINDOWS-SERVER $(cat <<'EOF'
echo hello
echo there
EOF
)

$ sh test.sh
hello
there

hello echo there

When I run the script test.sh below from an MSYS2 terminal on Windows the newlines are not honored in the ssh command. The remote shell is cmd and the file is saved with Windows line endings (CR LF). Any ideas?

$ cat test.sh
text=$(cat <<'EOF'
hello
there
EOF
) 
printf "%s\n\n" "$text"

ssh A-WINDOWS-SERVER $(cat <<'EOF'
echo hello
echo there
EOF
)

$ sh test.sh
hello
there

hello echo there
Share Improve this question asked Jan 23 at 19:16 August KarlstromAugust Karlstrom 11.4k8 gold badges44 silver badges69 bronze badges 2
  • What environment does your Windows ssh server provide? The normal Windows shell doesn't understand "cat". – Tim Roberts Commented Jan 23 at 19:20
  • @TimRoberts It's only the MSYS2 shell which will execute the cat commands. – August Karlstrom Commented Jan 23 at 19:28
Add a comment  | 

1 Answer 1

Reset to default 2

The problem here actually occurs in the original script, not in the ssh command. Remember that the $(...) gets executed locally, not remotely. Notice this, executed in Linux:

timr@Tims-NUC:~/src$ echo $(cat <<EOF
> hello
> there
> EOF
> )
hello there
timr@Tims-NUC:~/src$ 

What you're doing here is converting words from the "file" into command line arguments. The white space all gets removed. Your command ends up being

ssh A-WINDOWS-SERVER echo hello echo there

and that's exactly what you see.

If you want to pass multiple commands through the ssh, pass them via stdin, not via the command line.

本文标签: msys2Why are newlines not honored in SSH command on WindowsStack Overflow