16

I am trying to execute a task using Azure CLI and bash in Azure Devops. I am using Azure CLI task v.2 and choosing shell as script type as below. enter image description here I want to use pipeline variables in bash script. I run the command below inside the script:

#!/bin/bash
az role assignment create --role "Lab Admin" --assignee $(ownerEmail) -g $(rgName)

and i got the error below:

 line 2: ownerEmail: command not found
 line 2: rgName: command not found

I don't understand. Normally, i should be able to use azure cli in a bash script. Does anybody have an idea?

2 Answers 2

18

There are a couple of ways you could read the pipeline variables in your bash script:

  1. (Suggested) Read them straight as environment variables in the script. Azure DevOps pipeline variables are added as environment variables that can be accessed by your bash script. So just by defining/setting the pipeline variables you can access them from the bash script. i.e in your script reference them as $OWNERNAME and $RGNAME.
    See > https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables#environment-variables

  2. Reference the direct argument in the bash script. Bash arguments are numbered and can be referenced as such. e.g for your aruments $1 is the string "-labOwner" $2 is the value contained within the ownerEmail pipeline variable.
    See > https://tecadmin.net/tutorial/bash-scripting/bash-command-arguments/

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

Comments

16

Variables are passed in as environment variables. The variables are all uppercased and case-sensitive on linux.

Thus, $OWNERNAME and $RGNAME are likely the values you're after.

See the section on variable usage in scripts:

There are some tasks that don't populate the environment at all, these have an environment section where you can manually pass in the environment variables.

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.