YAO
YAO

Reputation: 475

Running csh script from bash script

I am writing a script that requires a initial setup. the setup is in the form of csh script that has many artifacts on the environment variables. right now when i'm executing the csh from within the bash, the variables inside the subshell of the bash are left unchanged.

example:

#!/bin/bash 
echo $PATH
setevnvar.csh -dir $ROOT_DIR/
echo $PATH

in this example I would to see that the PATH variable is changed after running the csh script (it is one of the results)

would appreciate any thoughts.

Upvotes: 0

Views: 4032

Answers (1)

Fred
Fred

Reputation: 6995

It is not possible to modify the variables of a shell from any child process. Since launching csh from bash launches a child process, there is no way that can be done.

Options you have :

  • Convert your csh script to bash, and source it from your bash script.
  • Convert your bash script to csh, and again source the other script
  • Make sure the variables you need are marked for export in the csh script, and launch your bash script from inside the csh script (which may or may not work for your specific need), thereby turning things inside out
  • Merge the code from both scripts to have a single (bash or csh) script

"Sourcing" is done with a . or the (non-POSIX) source builtin. For instance :

#!/bin/bash 
echo $PATH
. setevnvar.converted_to_bash -dir "$ROOT_DIR/"
echo $PATH

"Sourcing" causes the current process to read commands from an other file and execute them as if they were part of the current script, rather than starting a new shell to execute that other file. This is why variable assignments will work with this method.

Please note I added double quotes to your "$ROOT_DIR/" expansion, to protect for the case where it would contain special characters, like spaces.

Upvotes: 1

Related Questions