I'm trying to update environment variables using a bash script. but the variables are not being udpated.
Please see script below:
echo "version ~> $VERSION"
echo "build ~> $BUILD"
echo "##vso[task.setvariable variable=version;]2.0.0"
echo "##vso[task.setvariable variable=build;]20"
echo "Version ~> $VERSION"
echo "Build ~> $BUILD"



@carvalho-oak ##vso[task.setvariable] change the variables in the agent's context, the change will show up in your following task.
in your sample, if you add another bash script task and let it run printenv|sort, you will see your new $Build/$Version
@TingluoHuang, thanks for your reply. If I understood it correctly what you saying is that by updating my variables in the bash script, will just make the updated variables available in the subsequent tasks?
Should it also update the pipeline variables in the variables config? So in my first screenshot the variables "version" and "build" should also be updated with the new values after running the script?
@carvalho-oak Yes, ##vso[task.setvariable] can define new variable or change existing variable in your running job. just like Dictionary["foo"]="bar"
@TingluoHuang, So I am probably missing something I guess because the script never prints the update value for the variables. The script is setting the variables to version="2.0.0" and build="20", so it should print these values the second time I ran the script, but it always print the original values? Any idea?





@TingluoHuang, thanks again for putting this example up. I got it working, but my question now is, should also the environment variable updated after the script run? I mean, after running the script should the variable, the one highlighted in yellow on you screen, change to 2.0.0?
@TingluoHuang ping
which environment variable? version is a variable in agent's context, we expose the variable through environment variable to user/task/script.
I have the same problem and I have spent pass two days trying to solve it.
As the subject is saying it is wrong on bash but the example working here is in CMD.
The modified pipeline variable in bash is not set neither in the same task nor following task.
Bash script 1
echo ##vso[task.setvariable variable=ACR_REGISTRY_LOGIN;]$ACR_REG
echo "==============="< /br>
echo "ACR reg login"
echo $ACR_REGISTRY_LOGIN
echo "==============="
$ACR_REGISTRY_LOGIN is not set on this script nor following task scripts.
I have tried with almost every variation:
echo ##vso[task.setvariable variable=ACR_REGISTRY_LOGIN;]$ACR_REG
echo "##vso[task.setvariable variable=ACR_REGISTRY_LOGIN;]$ACR_REG"
echo "##vso[task.setvariable variable=ACR_REGISTRY_LOGIN;]$(ACR_REG)"
echo '##vso[task.setvariable variable=ACR_REGISTRY_LOGIN;]$ACR_REG'
and so on...
Any idea how to solve it?
I don't want to use cmd or powershell if I can avoid it.
The build is running on Hosted VS2017 so I think it is git-bash.
@ferpega # is comment in bash, you need to escape it. ##vso[task.setvariable] is a instruction to let the agent update its variable collection, so following task can consume the variable.
the following should work.
task1: echo '##vso[task.setvariable variable=ACR_REGISTRY_LOGIN;]$ACR_REG'
task2: echo $ACR_REGISTRY_LOGIN
@TingluoHuang I have set a simple build-pipeline with two bash task, in this way:
Remember it is running on Hosted VS2017
export ACR_REG="Hello world"
echo "==============="
echo "ACR_REG"
echo "$ACR_REG"
echo "==============="
echo '##vso[task.setvariable variable=ACR_REGISTRY_LOGIN;]$ACR_REG'
echo "==============="
echo "ACR reg login"
echo $ACR_REGISTRY_LOGIN
echo "==============="
With its results:
2019-01-25T08:24:40.0397075Z ##[section]Starting: Set variable
2019-01-25T08:24:40.0401866Z ==============================================================================
2019-01-25T08:24:40.0401932Z Task : Bash
2019-01-25T08:24:40.0401970Z Description : Run a Bash script on macOS, Linux, or Windows
2019-01-25T08:24:40.0402023Z Version : 3.142.2
2019-01-25T08:24:40.0402059Z Author : Microsoft Corporation
2019-01-25T08:24:40.0402095Z Help : [More Information](https://go.microsoft.com/fwlink/?LinkID=613738)
2019-01-25T08:24:40.0402131Z ==============================================================================
2019-01-25T08:24:40.4703091Z Generating script.
2019-01-25T08:24:40.4905516Z [command]"C:\Program Files\Git\bin\bash.exe" --noprofile --norc -c pwd
2019-01-25T08:24:42.6313624Z /d/a/_temp
2019-01-25T08:24:42.6396431Z [command]"C:\Program Files\Git\bin\bash.exe" --noprofile --norc /d/a/_temp/82aaa064-db0c-466d-a054-40b4fb3ed973.sh
2019-01-25T08:24:42.7023185Z ===============
2019-01-25T08:24:42.7026920Z ACR_REG
2019-01-25T08:24:42.7027014Z Hello world
2019-01-25T08:24:42.7027046Z ===============
2019-01-25T08:24:42.7055567Z ===============
2019-01-25T08:24:42.7055631Z ACR reg login
2019-01-25T08:24:42.7055653Z
2019-01-25T08:24:42.7055685Z ===============
2019-01-25T08:24:42.7133191Z ##[section]Finishing: Set variable
In this results, IIRC, $ACR_REGISTRY_LOGIN still has not its value set.
It will set on next task.
But not either:
echo "==============="
echo "ACR reg login"
echo $ACR_REGISTRY_LOGIN
echo "$ACR_REGISTRY_LOGIN"
echo "==============="
And with its results too:
2019-01-25T08:24:42.7148103Z ##[section]Starting: Show Variable
2019-01-25T08:24:42.7150881Z ==============================================================================
2019-01-25T08:24:42.7151127Z Task : Bash
2019-01-25T08:24:42.7151154Z Description : Run a Bash script on macOS, Linux, or Windows
2019-01-25T08:24:42.7152136Z Version : 3.142.2
2019-01-25T08:24:42.7152179Z Author : Microsoft Corporation
2019-01-25T08:24:42.7152234Z Help : [More Information](https://go.microsoft.com/fwlink/?LinkID=613738)
2019-01-25T08:24:42.7152270Z ==============================================================================
2019-01-25T08:24:42.9415279Z Generating script.
2019-01-25T08:24:42.9572335Z [command]"C:\Program Files\Git\bin\bash.exe" --noprofile --norc -c pwd
2019-01-25T08:24:42.9820476Z /d/a/_temp
2019-01-25T08:24:42.9885536Z [command]"C:\Program Files\Git\bin\bash.exe" --noprofile --norc /d/a/_temp/b780b9c3-e4b1-47ed-831a-6429271118c3.sh
2019-01-25T08:24:43.0103235Z ===============
2019-01-25T08:24:43.0114870Z ACR reg login
2019-01-25T08:24:43.0115492Z $ACR_REG
2019-01-25T08:24:43.0115632Z $ACR_REG
2019-01-25T08:24:43.0115737Z ===============
2019-01-25T08:24:43.0214051Z ##[section]Finishing: Show Variable
@ferpega
leave $ACR_REG out of the quote.
echo '##vso[task.setvariable variable=ACR_REGISTRY_LOGIN;]'$ACR_REG
##vso is a STDOUT instruction to the agent, it don't mean anything to your current bash session, so it won't change variable in the current bash session.
the agent process everything base on STDOUT, so you need to unwrap all the escaping/indirect reference problem.
I have solved it in this way:
echo "##vso[task.setvariable variable=ACR_REGISTRY_LOGIN]$ACR_REG"
1.- With double quotes in echo command.
2.- Removing semicolon at the end of variable name.
Regards.
@ferpega cool, btw i don't think you have to remove the ;
@TingluoHuang I am a bit tired of try things.. ;-)
In this way it runs.
Any reason to keep this open?
@bryanmacfarlane the issue was created by a different customer, i haven't get response on his issue.
we can re-open if someone has a response or details that the above answer doesn't cover
@TingluoHuang @bryanmacfarlane It would be nice to update the documentation on this matter, I have myself spent a few hours trying to figure it out.
Both are working for me:
export OLD_VARIABLE="Hello World"
echo '##vso[task.setvariable variable=new_variable;]'$OLD_VARIABLE
# and
export OLD_VARIABLE="Hello World"
echo "##vso[task.setvariable variable=new_variable;]$OLD_VARIABLE"
To bring up an old issue here but I'm struggling with this as well and I think it's what @carvalho-oak was getting at (sorry if I assume wrong).
When updating the environment variable I would expect, in the variables tab on the pipeline, to be updated as well.
Taking @TingluoHuang original image, this is what we expect before the pipeline has run

Once that pipeline is complete I would expect this...

Thus, each time that pipeline runs the version gets updated.
This currently isn't the case and whilst the environment variable is updated during that given pipeline, the next pipeline reverts back to v1.0.0.
Hope that makes sense and what the struggle is now.
@TingluoHuang @bryanmacfarlane It would be nice to update the documentation on this matter, I have myself spent a few hours trying to figure it out.
Both are working for me:
export OLD_VARIABLE="Hello World" echo '##vso[task.setvariable variable=new_variable;]'$OLD_VARIABLE # and export OLD_VARIABLE="Hello World" echo "##vso[task.setvariable variable=new_variable;]$OLD_VARIABLE"
I cannot tell you how much this has saved my bacon :) amazing thank you!!
To bring up an old issue here but I'm struggling with this as well and I think it's what @carvalho-oak was getting at (sorry if I assume wrong).
When updating the environment variable I would expect, in the variables tab on the pipeline, to be updated as well.
Taking @TingluoHuang original image, this is what we expect before the pipeline has run
Once that pipeline is complete I would expect this...
Thus, each time that pipeline runs the version gets updated.
This currently isn't the case and whilst the environment variable is updated during that given pipeline, the next pipeline reverts back to v1.0.0.
Hope that makes sense and what the struggle is now.
This problem was fixed?
I'm trying to update but not working too.
If you fixed, can you explain how you fixed ?
I never managed to fix it, I ended up going with a different variation on the solution outside control of the pipeline.
same issue here and tried everything I can find, no luck.
I am not able to get it working either. lost half day already. Tried running the bash task (where I have the below commands) on windows and mac agents with the below syntax , none worked so far. Any help greatly appreciated
export oldVariable="OMG"
echo '##vso[task.setvariable variable=latestVariable;]$oldVariable'
echo "##vso[task.setvariable variable=latestVariable;]$oldVariable"
echo ##vso[task.setvariable variable=latestVariable;]$oldVariable
I get an empty output when I try printing $latestVariable
Hi, I also have this issue it is still not working
I tired this combinations:
Using hosted Ubuntu Agent 20.04
echo '##vso[task.setvariable variable=latestVariable;]$oldVariable'
echo '##vso[task.setvariable variable=latestVariable;]'$oldVariable
echo "##vso[task.setvariable variable=latestVariable]$oldVariable"
echo "##vso[task.setvariable variable=latestVariable;]$oldVariable"
echo ##vso[task.setvariable variable=latestVariable;]$oldVariable
Can you please reopen this ?
This does not work, I have also the same issue. This is happening in Bash@3 task in yaml pipeline.
REOPEN! This still a bug
@WolfieZero @douglasoliveirabh currently pipeline variables are working in one direction - you can specify variable values before build run, but these values won't be affected by pipeline run in UI.
If you want some change on this - could you please create ticket on https://developercommunity.visualstudio.com/spaces/21/index.html to get right eyes on it?
@xmenge @lis-maria @keamas @naushad0502 @cforce the following example should work (I checked it and it works for me also):
task 1: echo '##vso[task.setvariable variable=NEW_VARIABLE;]'$OLD_VARIABLE
task 2: echo $NEW_VARIABLE
Please note that for Bash task letters case make sense - so if you specified variable name in in lower case in vso command like below:
task 1: '##vso[task.setvariable variable=new_variable;]'$OLD_VARIABLE
you need to specify it in upper case for further tasks:
task 2: echo $NEW_VARIABLE
Let me close this issue at the moment - feel free to ask further questions!
but
This is a trap for any user. You shall make that less error prone supporting both.
@carvalho-oak
##vso[task.setvariable]change the variables in the agent's context, the change will show up in your following task.
in your sample, if you add another bash script task and let it runprintenv|sort, you will see your new $Build/$Version
THIS WAS IT. Any other nonsense with/without semicolon and with single or double quotes is a red herring. Do what the documentation says and add a separate task after to check the value.
Most helpful comment
I have solved it in this way:
echo "##vso[task.setvariable variable=ACR_REGISTRY_LOGIN]$ACR_REG"1.- With double quotes in echo command.
2.- Removing semicolon at the end of variable name.
Regards.