Set bash command output into an azure yml variable

Viewed 5285

I'm working with Azure DevOps and I need to set the return of bash commands into some variables, for example I have the following:

    variables:
      VERSION: 7.2 # works fine
      FILE_VERSION: ${{cat public/VERSION}} # syntax error

I tried some some variations from ${{}} without success and I could not found the right syntax for it, but I think it must be possible.

3 Answers

You should use a bash step for that.

Like this:

steps:
- bash: |
    echo "##vso[task.setvariable variable=FILE_VERSION]$(cat public/VERSION)"

We shouldn't use cat command when defining variables. The command should be located in tasks/steps.

According to your description, I assume you're trying to pass the content of public/7.2 to FILE_VERSION variable. Here's my test:

1.Azure Devops Git repo:

enter image description here

2.Define the VERSION variable:

variables:
      VERSION: 7.2

Run the cat command and set job-scoped variable:

- bash: |
          FILE_VERSION=$(cat public/$(VERSION))
          echo "##vso[task.setvariable variable=FILE_VERSION]$FILE_VERSION"

The whole yaml:

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

variables:
      VERSION: 7.2

steps:
- bash: |
          FILE_VERSION=$(cat public/$(VERSION))
          echo "##vso[task.setvariable variable=FILE_VERSION]$FILE_VERSION"
          
#Use second bash task to test the variable.
- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      echo $(FILE_VERSION)
Related