Take the 2-minute tour ×

In my environment (RHEL) I have a bunch of script that are written in either csh or bash.

Some of them are used to deal with a tomcat server.

I want to keep the definitions of CATALINA_HOME and CATALINA_BASE in one place, such that csh and bash script can source the variables from one single file.

I am more familiar with bash, and has very limited exposure to csh. So for the bash scripts it is what I will do:

. ~/tomcat.settings

And in tomcat.settings, I will have

export CATALINA_HOME=/mnt/apps/tomcat/6.0.20/linux
export CATALINA_BASE=/home/app1/server

How can I source from the same file in csh?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Don't try to make the same script portable between two completely different shells. Even compatibility of sh and bash is hard to achieve, and those use the same syntax... (I can't even imagine how one can stand csh in the first place.)

Write your settings scriptlet like this:

test "$?BASH_VERSION" = "0" || eval 'setenv() { export "$1=$2"; }'

setenv CATALINA_HOME "/mnt/apps/tomcat/6.0.20/linux"
setenv CATALINA_BASE "/home/app1/server"

I tested this in bash 4.2.20, tcsh 6.17 and dash 0.5.7.

Somewhat more sane solutions:

You could keep the settings as plain text, like this:

  • ~/tomcat/catalina-home

  • your bash scripts

    export CATALINA_HOME=$(< ~/tomcat/catalina-home)
    export CATALINA_BASE=$(< ~/tomcat/catalina-base)
  • your csh scripts

    setenv CATALINA_HOME "`cat ~/tomcat/catalina-home`"
    setenv CATALINA_BASE "`cat ~/tomcat/catalina-base`"

You could write a wrapper that would set up the environment:

  • /usr/bin/tcat

    # you can use any, *any* language for this one
    export CATALINA_BASE=/home/app1/server
    exec "$@"
  • if you had myscript, you'd run it like tcat myscript args args args

You could combine the above two approaches by setting up an "env directory" containing the raw data and running your script via Dan Bernstein's envdir or Gerrit Pape's chpst:

  • echo "/mnt/apps/tomcat/6.0.20/linux" > ~/tomcat-envdir/CATALINA_HOME
    echo "/home/app1/server" > ~/tomcat-envdir/CATALINA_BASE
    envdir ~/tomcat-envdir/ myscript
    chpst -e ~/tomcat-envdir/ myscript
share|improve this answer

Your Answer


