0

My local machine has Windows OS. My remote machine has Linux OS. I'm trying to run some bash commands on the remote machine from my local machine (with the use of ssh). In the powershell script below I'm feeding a multiline comment to stdin input of ssh client. The code does not work and gives me an error. The problem is that each line passed to bash has \r (carriage return symbol) appended to it and bash interprets it as a part of a command. What are my options here?

Powershell script

ssh -tt "$Username@$Host" @"
    cd /home/someuser/project
    cd ~
"@

Bash error

bash: line 0: cd: $'/home/someuser/project\r': No such file or directory
3
  • 1
    Since you are asking about tunning bash, why put this on a PowerShell Q&A? Are you saying you want to run your bash command in a PS remoting script? There are many SO Q&A's the cover running bash from PowerShell. Just use the SO search box above. As well as many bash via PowerShell all over the web. What did you search for? What did you try? Specifically, when trying to do this via PowerShell. Just save your command as a .sh and run it from PowerShell. As long as you have your ENVIR variables properly set. Commented Aug 30, 2022 at 5:20
  • @postanote, hey! I should have been more clear in my question. Fixed the description. Sorry. Commented Aug 30, 2022 at 6:18
  • It works on my W10 computer with PS 5.1 and ssh installed through WSL (if I remember well). Remote tested servers are RHEL with bash also. According to your analysis, did you simply tried to replace your string ? @"..."@ -replace "`r`n", "`n" for instance (I guess it won't convert the last line however) Commented Aug 30, 2022 at 8:25

1 Answer 1

1

I assume you execute ssh from Windows PowerShell or PowerShell Core on Windows as it is using CRLF for line breaks. bash doesn't like CRLF. So, concatenate your commands with line delimiters and pass it to ssh in one line:

ssh -tt "$Username@$Host" "cd /home/someuser/project; cd ~"

If it is a larger script, you can maintain it in a file an convert it automatically to a (not really) "one-liner":

$myBashScript = (Get-Content 'my-bash-script.txt') -join ';'
ssh -tt "$Username@$Host" $myBashScript
Sign up to request clarification or add additional context in comments.

3 Comments

I fear that the second approach would cause issues when a script has if else statements.
@manidos Yes, it won't work for every complexity.
I did not test it, but Start-Process ssh -ArgumentList "-tt $Username@$Host" -RedirectStandardInput my-bash-script.txt might also be worth a look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.