Howto: Escape $(var) in Azure DevOps YAML

Viewed 7619
variables:
  buildSelected: '1.0.0.1234'

steps:
  - powershell: |
      Write-Host "Build Selected $(buildSelected)"
      Write-Host "Escaped '$(buildSelected)'"
    displayName: "Escape variable"

I would like the value 1.0.0.1234 & '$(buildSelected)' to be printed instead of what it's printing now:

Build Selected 1.0.0.1234
Escaped '1.0.0.1234'
2 Answers

Sorry but I'm afraid Azure Devops doesn't provide the feature to escape a pipeline variable. If the variable is used in this format $(var), it will always be replaced with its value when using Write-Host to output it.

As I know in Powershell syntax, only the ` can be used to escape variables. See:

Write-Host "Build Selected `$`(buildSelected)" 

Its output : Build Selected $(buildSelected)

Not sure if it's what you need, but escaping pipeline variables with complete $(var) is not supported. Azure Devops will always replace it with its value if it matches the $(var) format.

I had the same problem but in bash and solved it by adding the invisible character named "ZERO WIDTH SPACE" between "$" and "(". This way I can print out "$(Build.SourceVersion)" without it being replaced with the actual value.

I copied the character from https://invisible-characters.com/

---
trigger: none

steps:
  - script: |
      echo "$​(Build.SourceVersion): $(Build.SourceVersion)"
    displayName: Test Pipeline Variable Escaping

escape pipeline variable

Related