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
3
  • Depending on what the script does, it might be impossible.
    – muru
    Dec 10, 2015 at 22:17
  • @muru one could use awk to convert csh syntax of alias and variable to bash, right ? Dec 10, 2015 at 23:27
  • @Serg One could use a lot of things. But it maybe that there are csh features that have no bash equivalents. I don't know. Good luck writing a csh to bash compiler in awk.
    – muru
    Dec 10, 2015 at 23:28

3 Answers 3

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.

1
  • That's right. csh is a different language. You wouldn't expect bash to understand C or Python or ... Dec 10, 2015 at 2:55
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.

2
  • But I want to set the environment variables from the cshrc script. You method works for running the scripts but the variables set in that script will be lost after the execution of that script. I am asking how to source the csh script ( i.e. run it in the same shell not a child shell ) Dec 10, 2015 at 20:28
  • @NitishSrivastava sourcing is not possible , as far as I can tell. Syntax of bash is different than csh so any function defined in .cshrc file will likely be unintelligible to Dec 10, 2015 at 20:34
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/$/\"/'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .