Tcsh and Bash Initialization

I would like to be able to specify a file for setting some environment variables, but make it so that it does not depend on the shell used.

for instance

%: source START.env

# START.env
if [ $SHELL == "bash" ]; then
  source START.env.bash  # sets environment variables
else
  source START.env.tcsh  # sets environment variables
fi

However, this code will only work for bash. How can I make it compatible with bash and tcsh?

I need to transfer the file because I want environment variables to be inserted afterwards.

+3
source share
3 answers

So, I decided to use the full DSL approach to solve this problem (see clamshell ).

+1
source

Create your variable file for tcsh:

setenv FOO 42
setenv BAR baz
setenv BAZ "foo bar"

In your tcsh script use:

source filename

In your Bash script use:

while read -r cmd var val
do
    if [[ $cmd == "setenv" ]]
    then
        declare -x "$var=$val"
    fi
done < filename

script, tcsh, Bash.

+2

, . , .

( bash)

sed -i 's/setenv \([a-zA-Z0-9_]*\) /declare -x \1=/g' filename
source filename

( tcsh script)

sed -i 's/declare -x \([a-zA-Z0-9_]*\)=/setenv \1 /g' filename
source filename

(Note that this will actually change your source file to the appropriate syntax each time it starts.)

+1
source

Source: https://habr.com/ru/post/1762884/


All Articles