export variables beetween step
Asked by Giuseppe Sorce:

Hi I need to store some variables create on a step for next step.

```
- parallel:
    - step:
    
        script:
          - MY_VAR="hello word"
- step 
   - echo "MY_VAR $MY_VAR"
   - echo "MY_VAR ${MY_VAR}"
    
          
```

I dont't see MY_VAR on console...   


Answers:
author: Theodora Boudale
body: Hi [@Giuseppe Sorce](/t5/user/viewprofilepage/user-id/4599746),

Variables created in one step are not available in the next steps. We have a feature request for what you are asking in our issue tracker that you can vote for <https://jira.atlassian.com/browse/BCLOUD-15849>

A way to work around this issue would be to write the values in a file, pass that file as an artifact, and then source the file in the next step.

Please note that in order for this to work the two steps shouldn't be parallel steps, as parallel steps cannot use artifacts produced by steps in the same parallel set. I just mention that because I see the *parallel* keyword in the part of the yaml file that you posted here.

An example of how this would work is the following:

```
pipelines: 
  default: 
    - step: 
        script: 
          - echo export MY_VAR="hello word" >> build.env
        artifacts: 
          - build.env
    - step: 
        script: 
          - source build.env
          - echo "MY_VAR ${MY_VAR}"
```

Please feel free to let me know if that works for you and if you have any questions.

Kind regards,  
Theodora

comments: author: Giuseppe Sorce
body: Hi thank you,

it works.. but with some problems. when print or use MY_VAR i get only "hello" and not "hello world"..but i can delete space and i think it can works very well


author: Giuseppe Sorce
body: Other question.... i need to store other variables...

```
pipelines: 
  default: 
    - step: 
        script: 
          - echo export MY_VAR="hello word" >> build.env
        artifacts: 
          - build.env
    - step: 
        script: 
          - source build.env
          - echo "MY_VAR ${MY_VAR}"
    - step: 
       script: 
        - echo export MY_VAR2="hello word2" >> build.env
       artifacts: 
        - build.env
    - step: 
       script: 
        - source build.env
        - echo "MY_VAR2 ${MY_VAR2}"

```

is it possible ? or next echo replace the first ? or in the thid step have to source build.env?


author: Theodora Boudale
body: Hi [@Giuseppe Sorce](/t5/user/viewprofilepage/user-id/4599746),

++Regarding the issue with spaces in variables:++

I had tested that with a variable that had no spaces, I see the same issue when the variable has a value with a space like "hello word". I was able to solve this by encapsulating this in escaped quotations, like this:

```
- echo export MY_VAR=\""hello word\"" >> build.env
```

You can do the same with the other variable:

```
- echo export MY_VAR2=\""hello word2\"" >> build.env
```

++Regarding additional variables:++

I have tested your example yml file and I can see that it works well. The second echo command is added at the end of the build.env file, so both variables should be available in the fourth step.

However, if you want MY_VAR to be available in the third step as well, you should source the file build.env in that step. You need to source the file in every step where you want its variables to be available.

If you have any other questions, please feel free to let me know.

Kind regards,  
Theodora


Tags: bitbucket-cloud

bitbucket-pipelines