3

I am using bash shell but some of the scripts that I need to source are in csh format. Can somebody tell how I can source csh scripts from bash shell?

By sourcing I mean the sourced csh script should be able to set the environment variables. So, I want to do something like this in my ~/.bashrc:

source path/to/csh_script/script.cshrc
muru
  • 207,228

3 Answers3

2

bash won't run all csh scripts perfectly. There may be some basic overlap, like very basic bash scripts will run in sh/dash, but if it fails to run in bash (test with bash [file] ) then it's a no-go. Either

  • modify them to run in bash, or
  • run them with csh, called from the bash script, as if you're running them in a terminal, having them return a result value or do whatever they're supposed to. This would not leave any variables or functions available to the main bash script, as you've commented, so you'll either

    • have them do whatever they're supposed to do to external files, or
    • return a single value/string, or maybe
    • treat them like another program and read multiple lines of output into your bash script.

If you're just looking to source variables, it's theoretically possible to go through the csh script line-by-line and adding just the variables into the current bash shell/script. Something similar to this:

while read line
do
[stuff with $line]
done < /path/to/[the csh script to add]

I don't know how all the variables are laid out in the scripts you're sourcing, so it's up to you to decide what the [stuff with $line] should be.

  • If they're like bash, then maybe just grep any lines that begin with spaces or characters, have a = with optionally more characters with no spaces, so:

    echo "$line" | grep '^\s*\S*=\S*'

  • or as above with anything after the = with:

    echo "$line" | grep '^\s*\S*=.*'

But I think csh variables all start with set / setenv, so could find set/setenv lines, then throw out the set/setenv. Try as a starter:

echo "$line" | grep '^\s*\S*set.*=.*'

Those're just quick sample grep / regex patterns, see this & that & others for more info.

Or, if it's only a few of them, go through them manually & paste all the variables into bash/sh compatible files you can then source in bash with no problems.

Xen2050
  • 8,943
0

I don't know csh but I have a similar requirement for ksh. My hack solution was to

eval source <(ksh -c ". /path/to/my_script.ksh
env|sort)

I ran into issues with quoting and ended up putting in some grep patterns after the env to remove environment variables that I didn't want carried over but it kinda worked.

I found another useful bit here:

https://www.theunixschool.com/2010/07/how-to-access-child-shell-env-variable.html

This little bit of sed creates properly quoted export commands:

sed 's/^/export /;s/=/=\"/;s/$/\"/'
Shane
  • 1
0

The shebang line already takes care of that for you. When you run a script that has #! /bin/csh -f as first line, the system will recognize #! part as script, and load whatever interpreter is specified after it (in this case /bin/csh).

Alternatively, you could call the interpreter explicitly , with csh /path/to/script.csh.

Here's a small demo. My interactive shell is mksh and I am calling a simple csh script:

xieerqi:
$ cat bin/whiledemo.csh                                                                       
#! /bin/csh -f 
# demoloop.csh - Sample loop script 
set j = 1
 while ( $j <= 5 )
   echo "Welcome $j times"
   @ j++
 end

xieerqi:
$ echo $SHELL
/bin/mksh

xieerqi:
$ bin/whiledemo.csh                                                                           
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times

xieerqi:
$ csh bin/whiledemo.csh                                                                       
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times

Note that if you have even one space in front of #! , the #! won't be interpreted, hence the script will run with your interactive shell from which you call the script. Naturally, you can expect script to fail due to different syntax between script and what interactive shell expects. In the process of writing this answer, I encountered this exact error.

muru
  • 207,228