How can environment variables inherited to sub scripts in a script task?
Asked by Thomas Leitner:

In Bamboo job I run a perl script using a script task.

The name of the (perl) script is entered in the field "Script file", "Interpreter" is set to "Shell".

In the perl script another (shell) script is called that does not have the same environment than the perl script but a new one. Shell variables set in the perl script are not known in the called shell script.

What is to do to inherit the environment variables from the perl script to the shell script?


Answers:
author: Daniel Santos
body: Hi [@Thomas Leitner](/t5/user/viewprofilepage/user-id/2647584),

Can you share more details about the OS you are using and the script call?

Shells which are child processes of the Bamboo script task should have the Bamboo variables in them.

I've run the following test and it succeeded:

1. Created a repository with the following scripts:  
   **Script 1** (name: **perl_script1.pl** )

   ```
   use strict;
   use warnings;
   my $perl_var=$ENV{bamboo_var};
   print "Bamboo global var script 1: $perl_var";

   system("perl perl_script2.pl")
   ```

   **Script 2** (name: **perl_script2.pl**)

   ```
   use strict;
   use warnings;
   my $perl_var=$ENV{bamboo_var};
   print "Bamboo global var script 2: $perl_var";
   ```

2. Created a global variable called **var** with the value**hello world!!!**
3. Created a plan using the repository above
4. Added a script task calling the first script.

   ```
   perl perl_script1.pl?
   ```

The results were like:

```
01-Feb-2019 16:27:59 Bamboo global var script 1: hello world!!!
01-Feb-2019 16:27:59 Bamboo global var script 2: hello world!!!
```

<br />

Can you share how your perl script is calling the next script?  
Could you also try the example above to check if it will work for you?

comments: author: Thomas Leitner
body: Took a while but now I wrote a perl test module that produces the failure:

```
#!/usr/bin/perl -w

use strict;
use Test::More;

my $cmd = "tmp.csh";
my $tmp_filename = "tmp.txt";

if (open FILE, ">$cmd") {
print FILE <<'SCRIPT_END';
#!/bin/csh
echo $PATH 
SCRIPT_END
close FILE;
}
chmod 0775, $cmd;
system( "$cmd >& $tmp_filename" );

my @output = ();
if (open FILE, "<$tmp_filename") {
@output = <FILE>;
close FILE;
}
my $shell_path = "@output";
chomp $shell_path;

unlink $tmp_filename;
unlink $cmd;

ok( $shell_path =~ /^$ENV{PATH}$/, "check PATH variable in csh script call");

done_testing( 1 );

1;
```

Running in a shell the result is:

```
ok 1 - check PATH variable in csh script call
ok
All tests successful.
Files=1, Tests=1, 0.0655379 wallclock secs ( 0.00 usr 0.00 sys + 0.02 cusr 0.02 csys = 0.04 CPU)
Result: PASS
```

Running in Bamboo the result is:

```
26-Feb-2019 09:22:07 # Failed test 'check PATH variable in csh script call'
26-Feb-2019 09:22:07 # at Simple.t line 30.
26-Feb-2019 09:22:07 # Looks like you failed 1 test of 1.
26-Feb-2019 09:22:07 Simple.t .. 
26-Feb-2019 09:22:07 not ok 1 - check PATH variable in csh script call
26-Feb-2019 09:22:07 1..1
26-Feb-2019 09:22:07 Dubious, test returned 1 (wstat 256, 0x100)
26-Feb-2019 09:22:07 Failed 1/1 subtests 
26-Feb-2019 09:22:07 
26-Feb-2019 09:22:07 Test Summary Report
26-Feb-2019 09:22:07 -------------------
26-Feb-2019 09:22:07 Simple.t (Wstat: 256 Tests: 1 Failed: 1)
26-Feb-2019 09:22:07 Failed test: 1
26-Feb-2019 09:22:07 Non-zero exit status: 1
26-Feb-2019 09:22:07 Files=1, Tests=1, 0.148777 wallclock secs ( 0.01 usr 0.00 sys + 0.04 cusr 0.01 csys = 0.06 CPU)
26-Feb-2019 09:22:07 Result: FAIL
```


author: Daniel Santos
body: Can you share how exactly the task is configured to produce this result?  

I used your script without changing any line and got the exact same result from the terminal.  
I'm using Bamboo 6.7.2 on OSX (which should produce the same results you see on a Linux OS).


author: Thomas Leitner
body: We use [Bamboo](http://www.atlassian.com/software/bamboo/) version 5.14.1 build 51413 on a RedHead 2.6.32-431.29.2.el6.x86_64.

The Job has no specific setup. All tasks except the minimized script are disables (no code checkout, no testrunner, no postprocessing):

![bamboo.1.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/47154i93AA206E214B4AFB/image-size/large?v=v2&px=999 "bamboo.1.PNG")

The job fails and the log file is:

```
simple 27-Feb-2019 16:20:57 Build Digital_Development - Perl Unit Tests - Shell Calls #7 (DIGD-PUT-JOB1-7) started building on agent exsim01.elmos.de
simple 27-Feb-2019 16:20:57 Remote agent on host exsim01.elmos.de
simple 27-Feb-2019 16:20:57 Build working directory is /nfs1/qtr10034/common/run/unix/zusers/zbuild/Bamboo_exsim01_BuildAgent1/xml-data/build-dir/DIGD-PUT-JOB1
simple 27-Feb-2019 16:20:57 Cleaning directory /nfs1/qtr10034/common/run/unix/zusers/zbuild/Bamboo_exsim01_BuildAgent1/xml-data/build-dir/DIGD-PUT-JOB1
simple 27-Feb-2019 16:21:04 Executing build Digital_Development - Perl Unit Tests - Shell Calls #7 (DIGD-PUT-JOB1-7)
simple 27-Feb-2019 16:21:04 Skipping execution of task 'Checkout Default Repository' as it is disabled
simple 27-Feb-2019 16:21:04 Running pre-build action: VCS Version Collector
simple 27-Feb-2019 16:21:04 Running pre-build action: Hung Build Killer PreBuildAction
simple 27-Feb-2019 16:21:04 Skipping execution of task 'Checkout Default Repository' as it is disabled
simple 27-Feb-2019 16:21:04 Skipping execution of task 'Perl Testrunner' as it is disabled
simple 27-Feb-2019 16:21:04 Starting task 'Minimized Script' of type 'com.atlassian.bamboo.plugins.scripttask:task.builder.script'
command 27-Feb-2019 16:21:04 Beginning to execute external process for build 'Digital_Development - Perl Unit Tests - Shell Calls #7 (DIGD-PUT-JOB1-7)'\n ... running command line: \n/nfs1/qtr10034/common/run/unix/zusers/zbuild/Bamboo_exsim01_BuildAgent1/temp/DIGD-PUT-JOB1-7-ScriptBuildTask-1084222139899594308.sh\n ... in: /nfs1/qtr10034/common/run/unix/zusers/zbuild/Bamboo_exsim01_BuildAgent1/xml-data/build-dir/DIGD-PUT-JOB1\n ... using extra environment variables: \nbamboo_planRepository_1_rootUrl=http://svnhost.elmos.de/department/ds/ds_designflow/trunk\nbamboo_planRepository_1_branchDisplayName=trunk\nbamboo_repository_revision_number=2536\nbamboo_resultsUrl=https://buildmaster1.elmos.de/browse/DIGD-PUT-JOB1-7\nbamboo_planRepository_1_name=ds_designflow\nbamboo_build_working_directory=/nfs1/qtr10034/common/run/unix/zusers/zbuild/Bamboo_exsim01_BuildAgent1/xml-data/build-dir/DIGD-PUT-JOB1\nbamboo_buildKey=DIGD-PUT-JOB1\nbamboo_planRepository_1_branchPath=\nbamboo_shortPlanName=Perl Unit Tests\nbamboo_planRepository_name=ds_designflow\nbamboo_repository_svn_username=zbamboo\nbamboo_buildNumber=7\nbamboo_capability_system_jdk_JDK_1_8_0_172=/common/run/program/share/javatp/Linux2.6/latest\nbamboo_shortJobName=Shell Calls\nbamboo_buildResultsUrl=https://buildmaster1.elmos.de/browse/DIGD-PUT-JOB1-7\nbamboo_planRepository_url=http://svnhost.elmos.de/department/ds/ds_designflow/trunk\nbamboo_planRepository_repositoryUrl=http://svnhost.elmos.de/department/ds/ds_designflow/trunk\nbamboo_repository_15009645_svn_branchPath=\nbamboo_repository_15009645_svn_repositoryUrl=http://svnhost.elmos.de/department/ds/ds_designflow/trunk\nbamboo_agentId=8749058\nbamboo_planName=Digital_Development - Perl Unit Tests\nbamboo_shortPlanKey=PUT\nbamboo_shortJobKey=JOB1\nbamboo_planRepository_revision=2536\nbamboo_repository_previous_revision_number=2536\nbamboo_buildTimeStamp=2019-02-27T16:20:57.380+01:00\nbamboo_planRepository_previousRevision=2536\nbamboo_repository_svn_branchPath=\nbamboo_planRepository_branchPath=\nbamboo_buildResultKey=DIGD-PUT-JOB1-7\nbamboo_repository_branch_name=trunk\nbamboo_buildPlanName=Digital_Development - Perl Unit Tests - Shell Calls\nbamboo_planRepository_1_revision=2536\nbamboo_capability_system_hg_executable=/usr/bin/hg\nbamboo_capability_Synopsis=\nbamboo_repository_name=ds_designflow\nbamboo_repository_15009645_revision_number=2536\nbamboo_dependenciesDisabled=false\nbamboo_repository_15009645_previous_revision_number=2536\nbamboo_repository_15009645_svn_branchName=trunk\nbamboo_agentWorkingDirectory=/nfs1/qtr10034/common/run/unix/zusers/zbuild/Bamboo_exsim01_BuildAgent1/xml-data/build-dir\nbamboo_capability_system_git_executable=/usr/bin/git\nbamboo_planRepository_1_previousRevision=2536\nbamboo_repository_15009645_svn_rootUrl=http://svnhost.elmos.de/department/ds/ds_designflow/trunk\nbamboo_repository_svn_branchName=trunk\nbamboo_repository_15009645_branch_name=trunk\nbamboo_planRepository_rootUrl=http://svnhost.elmos.de/department/ds/ds_designflow/trunk\nbamboo_planRepository_branchDisplayName=trunk\nbamboo_capability_system_jdk_JDK_1_8_0_151=/common/department/dmosdesign/tools/software/java_jdk/jdk8/latest/\nbamboo_planRepository_1_type=svnv2\nbamboo_planRepository_branchName=trunk\nbamboo_repository_15009645_svn_url=http://svnhost.elmos.de/department/ds/ds_designflow/trunk\nbamboo_planRepository_1_url=http://svnhost.elmos.de/department/ds/ds_designflow/trunk\nbamboo_repository_15009645_svn_username=zbamboo\nbamboo_repository_svn_url=http://svnhost.elmos.de/department/ds/ds_designflow/trunk\nbamboo_capability_system_jdk_JDK=/common/department/dmosdesign/tools/software/java_jdk/jdk8/latest/\nbamboo_planRepository_type=svnv2\nbamboo_planRepository_1_username=zbamboo\nbamboo_ManualBuildTriggerReason_userName=tle\nbamboo_capability_system_builder_ant_Ant=/usr\nbamboo_working_directory=/nfs1/qtr10034/common/run/unix/zusers/zbuild/Bamboo_exsim01_BuildAgent1/xml-data/build-dir/DIGD-PUT-JOB1\nbamboo_planKey=DIGD-PUT\nbamboo_planRepository_1_repositoryUrl=http://svnhost.elmos.de/department/ds/ds_designflow/trunk\nbamboo_planRepository_username=zbamboo\nbamboo_repository_15009645_name=ds_designflow\nbamboo_repository_svn_rootUrl=http://svnhost.elmos.de/department/ds/ds_designflow/trunk\nbamboo_capability_system_jdk_JDK_1_8=/common/department/dmosdesign/tools/software/java_jdk/jdk8/latest/\nbamboo_repository_svn_repositoryUrl=http://svnhost.elmos.de/department/ds/ds_designflow/trunk\nbamboo_planRepository_1_branchName=trunk\n
build 27-Feb-2019 16:21:04 missing: /common/run/program/share/javatp/Linux2.6/latest/bin
build 27-Feb-2019 16:21:04 not ok 1 - check PATH variable in csh script call
build 27-Feb-2019 16:21:04 # shell: :/common/run/unix/svn/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/common/run/synopsys/bin:/common/run/mentor/bin:/common/run/unix/bin
build 27-Feb-2019 16:21:04 # perl: /common/run/program/share/javatp/Linux2.6/latest/bin::/common/run/unix/svn/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/common/run/synopsys/bin:/common/run/mentor/bin:/common/run/unix/bin
build 27-Feb-2019 16:21:04 # 
error 27-Feb-2019 16:21:04 # Failed test 'check PATH variable in csh script call
error 27-Feb-2019 16:21:04 # shell: :/common/run/unix/svn/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/common/run/synopsys/bin:/common/run/mentor/bin:/common/run/unix/bin
error 27-Feb-2019 16:21:04 # perl: /common/run/program/share/javatp/Linux2.6/latest/bin::/common/run/unix/svn/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/common/run/synopsys/bin:/common/run/mentor/bin:/common/run/unix/bin
error 27-Feb-2019 16:21:04 # '
error 27-Feb-2019 16:21:04 # at /nfs1/qtr10034/common/run/unix/zusers/zbuild/Bamboo_exsim01_BuildAgent1/temp/DIGD-PUT-JOB1-7-ScriptBuildTask-1084222139899594308.sh line 30.
build 27-Feb-2019 16:21:04 1..1
error 27-Feb-2019 16:21:04 # Looks like you failed 1 test of 1.
simple 27-Feb-2019 16:21:04 Failing task since return code of [/nfs1/qtr10034/common/run/unix/zusers/zbuild/Bamboo_exsim01_BuildAgent1/temp/DIGD-PUT-JOB1-7-ScriptBuildTask-1084222139899594308.sh] was 1 while expected 0
simple 27-Feb-2019 16:21:04 Finished task 'Minimized Script' with result: Failed
simple 27-Feb-2019 16:21:04 Skipping execution of task 'Test Parser' as it is disabled
simple 27-Feb-2019 16:21:04 Running post build plugin 'Docker Container Cleanup'
simple 27-Feb-2019 16:21:04 Running post build plugin 'NCover Results Collector'
simple 27-Feb-2019 16:21:04 Running post build plugin 'Clover Results Collector'
simple 27-Feb-2019 16:21:04 Running post build plugin 'npm Cache Cleanup'
simple 27-Feb-2019 16:21:04 Running post build plugin 'Artifact Copier'
simple 27-Feb-2019 16:21:04 Finalising the build...
simple 27-Feb-2019 16:21:04 Stopping timer.
simple 27-Feb-2019 16:21:04 Build DIGD-PUT-JOB1-7 completed.
simple 27-Feb-2019 16:21:04 Running on server: post build plugin 'NCover Results Collector'
simple 27-Feb-2019 16:21:04 Running on server: post build plugin 'Build Hanging Detection Configuration'
simple 27-Feb-2019 16:21:04 Running on server: post build plugin 'Clover Delta Calculator'
simple 27-Feb-2019 16:21:04 Running on server: post build plugin 'Maven Dependencies Postprocessor'
simple 27-Feb-2019 16:21:04 All post build plugins have finished
simple 27-Feb-2019 16:21:04 Generating build results summary...
simple 27-Feb-2019 16:21:04 Saving build results to disk...
simple 27-Feb-2019 16:21:04 Logging substituted variables...
simple 27-Feb-2019 16:21:04 Indexing build results...
simple 27-Feb-2019 16:21:04 Finished building DIGD-PUT-JOB1-7.
```


author: Thomas Leitner
body: The problem seems to be located in the .cshrc of the user starting the bamboo job.

Unfortunately I do not have access to that user setup files.

Thanks for the support. The remaining is an issue I have to discuss with our Bamboo administrators.


author: Daniel Santos
body: Hi [@Thomas Leitner](/t5/user/viewprofilepage/user-id/2647584)

For some reason, I missed your update from Wednesday, sorry about that.  
I'm glad that you found out at least where the problem is located. Thank you very much for sharing your findings here and accepting this answer.

Have a good one!