Programming in C shell is considered harmful by some people, but csh scripts are continued to be written by some hardcore csh fans. Is there a free/open source converter to translate csh script into bash?

If such a program does not exist, is it possible (from a purely technical consideration) to write one?

  • 1
    Well, your second question is fairly easy: yes, it's possible to write one: bash is Turing complete. Now, converting to idiomatic bash, much harder. But that's off-topic for Unix & Linux, I'd think.
    – derobert
    Commented Dec 20, 2016 at 19:50
  • 1
    I was thinking along the lines of traversing the parse tree in csh to generate bash source code. Not sure how readable the generated code might turn out to be. Seems like an interesting problem :)
    – Gowtham
    Commented Dec 21, 2016 at 10:49
  • Getting it to be readable but awkward is probably a sane amount of work, then going for idiomatic is where all the time will go. Of course, as long as you're not trying to source the script, you can freely call csh scripts from bash (and vice versa), so there isn't really a need to convert them most of the time.
    – derobert
    Commented Dec 21, 2016 at 13:24
  • (You could also try csh to Perl, idiomatic would be much harder, but just "it works" would probably be easier, especially if you use IPC::Run).
    – derobert
    Commented Dec 21, 2016 at 13:26

1 Answer 1


I have two very distinct parts to this answer. I'll start with the slightly more technical bit, and then with more of personal thought (ok, rant).

The bash shell comes with a cshtobash script hidden away in the examples/misc directory of its source distribution.

This will not take a csh script and convert it into bash, but it may possibly be useful for someone as a first step in creating a prototype sh-style ~/.profile file from an already existing csh-style ~/.login file.

Input: Standard OpenBSD csh initialization file.

# $OpenBSD: dot.login,v 1.6 2015/12/15 16:37:58 deraadt Exp $
# csh login file

if ( ! $?TERMCAP ) then
        if ( $?XTERM_VERSION ) then
                tset -IQ '-munknown:?vt220' $TERM
                tset -Q '-munknown:?vt220' $TERM

stty    newcrt crterase

set     savehist=100
set     ignoreeof

setenv  EXINIT          'set ai sm noeb'

if (-x /usr/games/fortune) /usr/games/fortune

Output: Not quite the same thing for bash... but at least something.

# csh aliases

# csh environment variables

export USER='me'
export HOME='/home/me'
export PATH='/usr/bin:/bin:/usr/ucb:.'
export TERM='rxvt'
export SHELL='/bin/bash'
export EXINIT='set ai sm noeb'

# csh variables

set -o ignoreeof # ignoreeof
PS1='% '
prompt2='? '

# special csh variables converted to bash equivalents

Yes, C-shell programming is "considered harmful". That does not mean that you can't write C-shell shell scripts that are not harmful, or not robust. It also does not mean that every single C-shell script needs to be converted to bash. And it definitely doesn't mean that such a conversion should be done by an automatic tool! It just means it's an awkward language that has some really odd quirks, especially if you're more used to sh.

As with any two programming languages, when one falls out of fashion to the benefit of the other, this does not mean that existing software needs to be rewritten. It also doesn't mean that proficient speakers of the out-of-fashion language should stop speaking. It just means that this may not be the language to pick up if you're looking for a modern language to do shell scripting in.

On the other hand, if you're given the task of extending or in some other way modify a csh script, then there might be a reason to

  1. read the code
  2. understand it
  3. reimplement it in another language

... especially if you're not good at csh programming.

As for automatic translation: Why?

If the script is working and you have the interpreter for it on your system, there is no need for translate the code. Translating the code automatically is unlikely to generate "better" code, and it very likely won't generate maintainable code, and you'd have to spend time on verifying that the translated script behaves in the same way as the original script anyway.


You must log in to answer this question.

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