Say, I modify a lot of code that uses tcsh, but since a lot of people say that bash is more modern, I'd also like to learn bash along the way. Is it possible for me to use a script that contains both of these commands at the same time? Since this is for personal use, elegance is not an issue.


No, it is not possible.

The reason is that the shell reads in chunks of the file as it executes it, and a chunk might contain shell code that you want to run in a different shell, but since its already been read by the first shell, the second shell cant read it if it were to take over execution of the script. (This could work if the shell had the ability to lseek() the file, but shell languages just arent that advanced)

However there is a dirty way of doing this; zsh.
zsh has a command called emulate which lets you switch shell languages on the fly. But using zsh just to emulate tcsh and bash is just plain dirty, not to mention overly complex.


Also I wouldnt say bash is more modern. It's more widespread sure, but it adopts features at a very slow pace. Bash is more focused on compatibility than anything.

  • Adopting features at a "very slow pace" isn't really a "bad thing". Look at how many distros Debian has spawned, and some of those have then spawned countless others, but in so many cases, when people start running into unexpected behavior and want to get back to "doing things right", they take a pause, and start again from Debian as a base. – killermist Jun 19 '12 at 0:06

I wouldn't recommend mixing and matching shell types within a script.

  1. You lose efficiency in launching a shell to execute a command and return a result.
  2. One of the reasons to keep everything a script needs in-script is that that script has access to all of the variables declared and returned from functions, etc. Passing an entire variable stack to another shell just to execute a couple commands and then return, seems a bit counter-intuitive.

I would say your best bet would be to copy a known script and change the calling shell, and then see what breaks, so you know where the differences in syntax are that have to be worked out.

But, in short, trying to mix and match within a script just seems like a bad idea. (personally)


I agree with the other answers that it's not generally a great idea.

But you can invoke a shell from a script. If you have a script currently written to use tcsh and you want to use a few bash-specific features, you can invoke bash explicitly. For example, bash makes it much easier to redirect stdout and stderr independently.

#!/bin/tcsh -f

echo Hello, tcsh
bash -c 'echo Hello, bash 1>&2' # writes to stderr
echo Good-bye, tcsh

In the long run, though, it's probably better to translate the entire script to use bash.

Obligatory link: http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

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.