1

I am trying to collect an powershell script output in a shell script.

ssh user@host "powershell Test-Path -Path C:/myfile" will if execute in bash just print True . If I save it to variable like ps_result=$(ssh ... >&1)

ps_result now contains ps_result=$'False\r' which looks weird and I don't know what to do with it in an if statement.

I have two Questions:

  • Can I use powershell to print something more useful than whatever $'False\r' is?
  • How to handle such an output in an if statement if [ $ps_result = 'False\r' ]; does not work?

1 Answer 1

2

$'False\r' represents a string that contains the literal value False followed by a CR character (\r).

The $'...' notation is a special Bash string notation called ANSI C-quoting, which supports escape sequences, such as \r for CR.

The CR character stems from the fact that Windows PowerShell uses CRLF (\r\n) newlines, whereas Bash expects Unix-style LF newlines (\n), and therefore considers the CR part of the data when it trims trailing newlines.

You can simply use $'False\r' in your if statement as well:

if [[ $ps_result == $'False\r' ]];

As for preventing the CR characters in the output:

You cannot make PowerShell use LF-only newlines, so the best you can do is to remove the CR instances after the fact; e.g., via parameter expansion:

if [[ ${ps_result//$'\r'/} == 'False' ]];
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you $'..' this ANSI C-quoting was the missing link I didn't understand. Also the parameter expansion syntax is new to me.
Glad to hear it, helped, @samst. You're certainly dealing with an unusual combination of tools.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.