2

I'm importing some values from a csv file and using them to create a adb command for an Android intent with the following code.

Write-Host adb shell am start -a android.intent.action.VIEW '-d' '"https://api.whatsapp.com/send?phone=' $($c.number)'"'

This gives me an out put of:

adb shell am start -a android.intent.action.VIEW -d "https://api.whatsapp.com/send?phone= 12345678 "

How can I remove the spaces where the variable is concatenated to the string to give the output of:

adb shell am start -a android.intent.action.VIEW -d "https://api.whatsapp.com/send?phone=12345678"
2
  • As an aside: Write-Host is typically the wrong tool to use, unless the intent is to write to the display only, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, or redirect it to a file. To output a value, use it by itself; e.g, $value, instead of Write-Host $value (or use Write-Output $value); see this answer. To explicitly print only to the display but with rich formatting, use Out-Host. Commented Oct 25, 2022 at 22:54
  • Specifically, using Write-Host here obscures the distinction between actual spaces in the arguments and multiple arguments being separated with spaces. Commented Oct 25, 2022 at 22:56

2 Answers 2

2

Use string interpolation by switching to double quotes:

Write-Host adb shell am start -a android.intent.action.VIEW '-d' "`"https://api.whatsapp.com/send?phone=$($c.number)`""

Within double quotes, you have to backtick-escape double quotes to output them literally.

Sign up to request clarification or add additional context in comments.

Comments

2

zett42's helpful answer is unquestionably the best solution to your problem.


As for what you tried:

Write-Host ... '"https://api.whatsapp.com/send?phone=' $($c.number)'"'
  • The fact that there is a space before $($c.number) implies that you're passing at least two arguments.

  • However, due to PowerShell's argument-mode parsing quirks, you're passing three, because the '"' string that directly follows $($c.number) too becomes its own argument.

Therefore, compound string arguments (composed of a mix of quoted and unquoted / differently quoted tokens) are best avoided in PowerShell.

Therefore:

Write-Host ... ('"https://api.whatsapp.com/send?phone=' + $c.number + '"')

Comments

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.