'How to reference a variable in Azure Devops on an Linux Agent which contains a dash/minus/-

There is this question which asks about using variables with a minus on linux agents. I tried an indirect reference as suggested here by doing ${!RELEASE_ARTIFACTS__BUILDPROJECT_SOURCEBRANCH} but this does not work, and causes an error in my pipeline. I tried some other approaches using the env command but they too throw errors... Seems odd that this issue persists I guess not as much attention is put on Linux Agents as to Windows. But I am wondering if anyone has found a solution to this?



Solution 1:[1]

Here is some code on a Linux agent with variables with dashes.

Try running a bash task and running printenv which will show you all variables available in the linux agent.

Please clarify further what you are trying to do because I can see vars with dashes are working fine in the Linux agents.

pool: default

variables:
- name: test
  value: test-value-with-dashes
- name: test-name
  value: test-name-with-dashes
- name: System.Debug
  value: true

trigger:
- never

stages:
  - stage: approval_check
    jobs:
    - job: bash
      steps:
      - task: Bash@3
        inputs:
          targetType: 'inline'
          script: |
            # Write your commands here
            set -x
            echo $(test)
            echo $(test-name)
            echo "print all env vars"
            printenv

And here is the output of printenv you can see the vars available in the build agent.

echo test-value-with-dashes
echo test-name-with-dashes
echo 'print all env vars'
printenv
.... lots of env vars printed...
BUILD_SOURCEBRANCH=refs/heads/main
BUILD_ARTIFACTSTAGINGDIRECTORY=/home/james/_work/3/a
...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James