0

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.

10
  • 1
    A csh script (which should have an extension like .csh, if any, not .sh) cannot directly set variables in a bash script. Commented Jan 25, 2017 at 12:36
  • When you say is in the form of csh script that has many artifacts on the environment variables --> The environment variables under csh or bash? Commented Jan 25, 2017 at 12:39
  • the variables are set inside the csh Commented Jan 25, 2017 at 12:40
  • @YardenOren: So if I understand right, you want to run a csh script which changes some bash environment variables for you? Commented Jan 25, 2017 at 12:41
  • @Inian yes! i want the .csh to change the variables that are in the subshell of the .sh script Commented Jan 25, 2017 at 12:42

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks! so if I'll have two csh scripts, than the source command will affect the the outside shell (after the script terminates)?
You must have two scripts run by the same interpreter (can be csh if you want), but you must call the script by sourcing it, as shown above, if not you will have the same issue. "Sourcing" causes the current process to read commands from an other file, as if they were part of the current script, rather than starting a new shell to execute that other file.
As a matter of interest, source is not bash specific, it comes from csh! It's effect is unspecified in POSIX.
@cdarke Thanks for the clarification!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.