From your step 3 script, print this exact line to stdout: ##teamcity[setParameter name='env.PASSED_ID' value='the-id-here'] . TeamCity picks it up and the value is available as an env var in step 4 and later. Three gotchas the thread calls out: the parameter only becomes available in the NEXT step, never the one that sets it; the line must go to stdout with no extra formatting or quoting around it; and if the value contains spaces or quotes, escape them per the service-message rules or the parse silently breaks.

Context: Stack Overflow #8219493 (accepted answer, 77 votes): a build generates an id in step 3 that step 4 needs. Setting an env var in the step 3 script does not carry over, because each build step runs its own process. The accepted answer points to TeamCity's service messages: the script prints a specially formatted line to stdout and TeamCity turns it into a build parameter for the following steps.