Sourcing .cshrc (C shell) environment variables to bash

I have tried with the following:

csh -c 'source ~/.cshrc; exec bash' # works perfectly
(cat ~/.cshrc; echo exec bash) | csh # not working 

And, using sed, I successfully retrieved the environment variables from ~/.cshrc

sed -rn 's/setenv\s+(\S+)\s+(.*)$/export \1=\2/p' ~/.cshrc

but now stuck-up in sourcing it to the bash environment, where the below solutions do not work :frowning:

sed -rn 's/setenv\s+(\S+)\s+(.*)$/export \1=\2/p' ~/.cshrc | bash
source /dev/stdin < <(sed -rn 's/setenv\s+(\S+)\s+(.*)$/export \1=\2/p' ~/.cshrc)

You are over complicating things. Why not just write the variables out to a temporary file and source that temporary file?

Even simpler, just write a .myownfile containing the variables and source that file.

True, but would like to explore new stuffs :slight_smile:

Could anyone correct my code?

.cshrc is automatically sourced

csh -c 'exec bash'

You need eval

eval $(
sed -rn 's/^\s*setenv\s+(\S+)\s+/export \1=/p' ~/.cshrc
)
1 Like

Awesome!! :), so can I do anything for this piece of code

(cat ~/.cshrc; echo exec bash) | csh

in order to make it work?

...being it's just a more complicated version of what he did in one parameter, why? What's your goal here?

Using grep move all setenv statements into, say, .setenv. Next, add the following to your .bashrc:

setenv() { export $1="$2"}
. ~/.setenv

Edit your .cshrc file and comment out or delete your setenv statements, and add

source ~/.setenv

(or whatever the correct syntax is).

From now on keep all your environment definitions in .setenv and both csh and bash will be able to use them.

Andrew