BreakingExpress

Why I nonetheless love tcsh in spite of everything these years

I take into account myself a cheerful Bash person. However, once I began exploring Unix, it was on a proprietary Unix system that offered tcsh by default, so my earliest shell experiences have been on a contemporary model of the C shell (csh). That turned out to be a lucky accident as a result of tcsh was additionally the shell of selection on the movie studio the place I labored later in my profession.

To this present day, there are a number of duties I affiliate with tcsh, though there is no logical correlation there. I nonetheless use tcsh on not less than one system, if solely to remain in apply. I additionally hold it put in on all my methods to keep up compatibility with my very own tcsh scripts and to make sure I can launch it once I want to put in writing a script I want to have in tcsh.

The distinction between C shell and Bash

Tcsh is the trendy continuation of csh. When the C shell was launched method again in 1978, customers have been excited over its C-like syntax. C was a brand new programming language on the time, and extra importantly, it was what Unix was written in, so most Unix customers have been extra comfy with C than an arbitrary scripting language. For occasion, in Bash, this if loop makes a “secret” name to the check binary to guage the conditional assertion (the half in brackets):

v=1

if [ $v -gt zero ]
  then
  echo "verbose"
fi

Here’s the identical assertion in csh or tcsh, with out an exterior name to check as a result of the conditional assertion (the code in parentheses) makes use of csh’s built-in analysis:

set v=1

if ($v > 1) then
  echo "verbose"
endif

This demonstrates just a few issues, each good and unhealthy. For occasion, it is pure for a C programmer to kind a conditional in parentheses, and it is fascinating from a programmer’s perspective to have math analysis constructed into the executable. On the opposite hand, the if loop syntax has extra in frequent with Lua than C, which makes use of braces as delimiters:

// this doesn't work in Csh
if ( v>1 )

In a method, this properly sums up csh: it is clear, environment friendly, and acquainted for some, however quirky and inconsistent.

So why use it?

Tcsh for precision

I write extra C++ and Java than C code, so my curiosity in tcsh shares little with its historic declare to fame. However, I take pleasure in Lua, and in a method, I consider tcsh and different shells like I consider Lua and Python and even Markdown and Docbook. Both shells have benefit, and it is easy to level to the one which’s extra widespread, however there’s strict readability to the opposite. In reality, I really feel tcsh has a precision that many different shells lack.

Variables

You do not get the posh of typed variables in shells, however with tcsh, you possibly can not less than declare them with a key phrase. Strangely, there are a number of key phrases you need to use for the duty, however the one I settled on was set:

> set var=foo
> echo $var
foo

You can develop variables by typing the title of the variable (var on this instance) after which urgent Ctrl+X adopted by the $ (greenback) key. The instance above units var to foo.

As with many different shells, you possibly can checklist all set variables utilizing set alone with no arguments:

> set
time period    xterm
tty     pts/2
uid     1000
person    seth
var     foo
[...]

You can unset a variable utilizing the unset command:

> unset var
> set | grep var
>

Missing options is a characteristic

Maybe you have seen traces like this in a script:



The second line is a built-in operate to rework the contents of var into lowercase.

While further capabilities like these are infinitely helpful, I typically really feel they’re an invite to obfuscation. Shell scripting is just like the HTML of programming; it is one of many few severe languages you possibly can educate your self simply by studying different individuals’s code. That’s not essentially an excellent motive to surrender helpful capabilities, nevertheless it’s the rationale I attempt to keep away from a number of the cryptic shorthand widespread in varied languages. When writing in tcsh, I haven’t got the choice to make use of as a lot shorthand, so complicated string operations should be carried out with primary Unix instruments somewhat than by built-in shortcuts. I consider it leads to code that is simpler to learn, and that makes an enormous distinction to future contributors—and to future, forgetful me.

Built-in math

One of the issues I like about tcsh is its built-in @ math shortcut. Like most self-taught Unix customers, I stumbled upon bc, and I’ve regretted it ever since. Through no fault of its personal, bc is usually taught to customers as a command-line calculator when it is truly higher suited as a calculation language. Workarounds embody a tremendous 300-line calculator in pure Bash, or the let command in Bash, or the @ command in tcsh.

> @ n = ( 1 + 1 / 2 )
> echo $n
1

For very complicated math, it might be value learning bc or a very good Python mathematics library.

All the necessities

For me, tcsh strikes an ideal stability between stark simplicity and important options. It’s not a shell for all customers, and even for all functions, however in the event you’re trying to simplify your view of your text-based world, tcsh may present an attention-grabbing different.

Exit mobile version