I have a bash script that contains many common functions definitions for our Linux system.

Is it possible to source it and use functions from another shell flavor (csh and ksh) ?

  • 1
    csh doesn't have functions. Use scripts instead of functions. Then you can decide which interpreter to use for each and can call them from any shell. – Stéphane Chazelas Oct 27 '17 at 11:17
  • Yes . Stick to POSIX and if you must use non-POSIX features, test them on multiple shells and possibly write adaptors where they don't work (outside of POSIX, local is very portable). I have one larger shell project and it runs OK in like 7 shells. – PSkocik Oct 27 '17 at 11:56
  • The direction I've chosen , inspired by the answers, is to rewrite all cshell in bash (only a few survivors from another era on the system) and to adapt the functions sources so it can be interpreted by both ksh and bash. ATM only a few changes were a necessity. – Guillaume Oct 27 '17 at 16:20

It should be easy enough to create a wrapper script around the functions; for Bash:





exit $?


/path/to/functionWrapper.sh doSomething [<param1>] [...]
  • Good, but I think I'm stuck since I use in some function global variable to handle the return of a string value (not a very nice pattern, but I didn't found another proper way) – Guillaume Oct 27 '17 at 11:21
  • Well, a good reason to refactor... Try using temp files (or perhaps named pipes) instead - caller creates the temp file, passes the name as parameter and takes care of cleanup. – Murphy Oct 27 '17 at 11:23

As Stéphane Chazelas noted in the comments, csh and related tcsh do not have functions, hence you can't source the functions file you have anyway.

However, bourne-like shells often share syntax and functionality, and in fact bash borrowed a lot of functionality from ksh, but some of the features these shells have aren't compatible with dash - a default shell on Ubuntu and a closer one in behavior to original Bourne shell.

Another issue that comes up is not just about functions, but commands that functions might call. If you're using those functions on Ubuntu, and then suddenly go to FreeBSD, you'll quickly find out that utilities like find or ps often have options that either behave differently or missing.

So in order to make sure you can use functions, I would suggest two things: consider commands and portability of their options and two - write functions that stick to POSIX standard and original Bourne shell syntax as close as possible.

Alternatively, if you have the luxury to always install the shell you want (which might not happen if you don't have sudo privileges on the system you're working), then write your functions in one specific shell's syntax.

See When is it important to write portable scripts? for additional info.


In general, no.

In particular, csh (and tcsh) have a too different syntax from the sh-derived shells (sh, bash, ksh, zsh), so they would most likely not understand a sh script, or vice versa.

The sh shells can sometimes share functions or even whole scripts, but only if they are written for the "smallest common denominator shell", which is usually sh. This, however, is usually not a good idea.

Think of each shell as a separate scripting language, like Perl, Ruby and Python. Don't mix them, and don't generally assume that one interpreter can understand the source of another.


In specific for the C Shell, you cannot, since C Shell doesn't support functions. Apart that, C Shell uses different syntax. For example, the following C Shell snippets are different from what Bash would use.

if ( $days > 365 ) then
   echo This is over a year.
set i = 2
set j = 1
while ( $j <= 10 )
   echo '2 **' $j = $i
   @ i *= 2
   @ j++

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.