4

I have a bash script and a tcsh script, my tcsh scripts sets some environment variables and those variables are not available for bash subsequent steps after tcsh script execution. Any suggestion how to make environment variables set in tcsh available for bash.

Here is sample code.

Bash script:

$Naga> cat sample_bash.sh
#!/bin/bash
export SOURCE="NO SOURCE"
./sample_tcsh.csh
echo "Source value in bash is $SOURCE"

tcsh script

$Naga> cat sample_tcsh.csh
#!/usr/bin/tcsh -fvx
echo "SOURCE initially $SOURCE"
setenv SOURCE "MY DATASOURCE"
echo "SOURCE value in tcsh is $SOURCE"
exit(0)

Results

$Naga> ./sample_bash.sh
SOURCE initially NO SOURCE
SOURCE value in tcsh is MY DATASOURCE
Source value in bash is NO SOURCE
Naga
  • 444
  • 2
  • 7
  • 18

1 Answers1

1

There is no global environment. Each process has its own environment it can freely change. Changes in a process evironment propagate to new child processes, but0 do not affect any other exisring process.

Thus a shell cannot change its own environment by running an external program. A shell can execute a script without creating a new process though. This is called sourcing. So if a script modifies an environment variable, this affects the sourcing shell. However bash cannot source a tcsh script, and vice versa.

So if you have a tcsh script and a bash script, the only way to propagate environment changes from tcsh to bash is to have the tcsh script call the bash script. Not the other way around.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243