What Unix Shell Should I Use?
Disclosure: Your support helps keep the site running! We earn a referral fee for some of the services we recommend on this page. Learn more
In a computer operating system, a "shell" is an interface that allows the user to issue commands and access the system's services. Technically, a graphical user interface (GUI) like the Windows Desktop is a type of shell.
However, when most people talk about a shell they mean a command line interface (CLI). This is also known as a "terminal" or just "the command line." But mostly, it is just a shell.
You may be familiar with shells already. The Command Prompt in Windows and the Terminal app in Mac are examples of shells. Unix systems typically have something call Bash as their default shell. (It is also available for Windows, but is not the default.)
Why Use a Shell?
A Unix shell is its command processor. In the beginning, it was the only way that users had of interacting with the computer. Now, of course, there are other options — such as the graphical user interface (GUI).
So you might wonder if we need a shell at all. But if you watch a Unix user working in a GUI, you will notice that a lot of those open windows are, in fact, running Unix shells. Why? Because they are super powerful.
GUI vs Shell
What can you do in a graphical user interface? Click on things.
You can click on things to open them, start them, move them from one folder or directory to another. And it's all pretty easy to do. For much computing, they are great!
But can you rename thousands of files all at once? Can you search the text of every document for a particular pattern of numbers and words that make up an address? Find all the images that were taken with a particular camera during a certain period of time and then apply the same color-correcting filter to all of them?
Using a shell makes it possible to quickly do both routine and highly specialized tasks quickly and efficiently — it lets you take almost complete control of your computer.
Moreover, shells allow you to write scripts, or small programs, that can interact directly with the operating system, executing a series of commands, and automating tasks.
But there are a lot of shells to choose from. (One of their best aspects!) In this article, we are going to look in some depth at the three most important shells: C, Korn, and Bash.
Unix Shell History
In the early days of computing, you didn't type in commands and get output back. Instead, you gave it a bunch of punch cards to input information. At first, you got punch cards back as output. But very quickly, a modified typewriter was added to the computer for output. Exciting at the time, but still slow and interactive only on glacial timescales.
But progress was being made. Keyboards were added to computers — generally as part of a teletype, so both input and output were part of the same device. So a rudimentary kind of command-line was developed.
Eventually, monitors replaced printers for output. But this didn't change things all that much, except that output was faster.
It wasn't until 1964 that things really changed with the release of the Multics operating system for 36-bit mainframe computers — most especially the GE-600 series.
Multics was a time-sharing operating system, allowing multiple people to use the computer at the same time. But the big thing from our perspective is that is that the command processor (they called a "shell" following computer scientist Louis Pouzin), which had always been part of the operating system kernel, was now just an ordinary program.
This might not sound like a big deal, but it was a huge advancement for computer users because they weren't stuck with the single interface the operating system was built with.
When Ken Thompson and the rest of the gang at AT&T created Unix, they followed Multics, including having the shell as a regular program.
The first Unix shell was written by Thompson and so was named the Thompson shell. It had features that Unix users have come to love like pipes (putting the output of one program into the input of another) and redirects (putting the output of a string of programs into a file). But mostly, it was very limited.
But because the shell was just another program, it was a relatively simple matter to write and use another shell. After a few years, it became clear that the operating system needed something more. And so people moved away from the Thompson shell — first to the Mashey shell and then the Bourne shell, written by Stephen Bourne.
This was not the end, of course. Other shells would come, most notably: the C shell, the KornShell, and now pretty much the king of all Unix shells: Bash.
The first big competitor to the Bourne shell was the C shell or
csh as its program is named. It is still used by a lot of people today, even if it never quite lived up to its potential as a scripting language.
Shells vs Shell Scripting
It is important to realize the difference between a shell and shell scripting. A shell is the interactive environment — the command line that you type things into.
Shell scripting is writing a program that will get interpreted by that command-line environment as a series of commands being typed in one after another. (Note, however, that you can effectively write a shell script on the command-line.)
Many Unix users like the C Shell environment, and dislike the scripting language.
C Shell History
Unix users were very happy with the Bourne shell that was released in 1977. But Bill Joy had a different idea for a shell that he released the following year: the C shell.
The idea behind the C shell was to create a syntax that was more like the C programming language, which Unix was built on.
But it had a number of problems in this regard. It did, however, catch on as a command-line shell because it was easier to use with features like command histories (you could repeat old commends with just a couple of keystrokes) and directory stacks (so you could easily move around the file system).
However, the Bourne shell dominated for administrative scripting. Throughout the 1980s, however, both the Bourne and C shells were widely used.
As we will see, the Bourne shell went on to be updated. That also happened with the C shell in the form of the TENEX C Shell (
tcsh). If you use the C shell today, it will probably be in the form of
Using C Shell
There is a lot to learn about the C shell. And much of it is very exciting. So read on.
Accessing C Shell
None of the three major operating system families (Windows, Mac, and Linux) use
csh as the default shell.
Mac OS X and Linux
Mac OS X and most Linux distros include
tcsh. You can enter a C shell terminal by typing
tcsh at the command line. (Mac calls its built-in
csh. Most Linux distros use
tcsh but include a symbolic link so that typing either name works the same.)
If you do not have
csh already, you will need to install it.
The C shell isn't really native to the way Windows works. You are probably better sticking with its included Power Shell.
C Shell Script Files
If you want to execute
csh files directly, first you must make sure they are executable. Then you must specify that the
csh interpreter be used by placing the following on the first line of the file:
csh syntax is based on C, which makes it more conventional than Bourne code.
Here is a simple Bourne shell script:
#!/bin/sh let x=11 if [ $x -gt 10 ] then echo "X is greater than 10." fi
Notice that the comparison is being done by using
-gt, which runs a separate process, instead of an in-laguage comparison operator. Also, the
if statement is terminated by the reversed-if
Here's the same thing in
#!/bin/csh set x=11 if ($x > 10) then echo "X is greater than 10." endif
Both code fragments are easily understood, but for C programmers, the
csh code is more natural.
C Shell Resources
There are a huge number of resources for learning the C Shell. We've collected the best ones.
Online Tutorials and Introductions
- The Grymoire C Shell Tutorial: Bruce Barnett's thorough introduction to the C shell.
- C Shell Scripting: a WikiBooks introduction to using the C Shell.
- Using the Tcsh Shell: the C shell section from Mac OS X for Unix Geeks.
- Linux Csh Command: another good, detailed technical reference, a little easier to read than the one above.
There are not a lot of books specific to the C shell, but there are a couple of good, if old, ones.
- Using csh & tcsh (1995) by Paul DuBois: this is a classic book on working with the C shell, and it is no less relevant today than it was when first published. Be sure to check out the online archive, where you can read sections of the book before buying it. Note that the author is strongly against using
cshfor scripting (he prefers Bash or Perl), so the book is really about using the C shell terminal, not about scripting.
- The Unix C Shell Field Guide (1986) by Gail Anderson: another classic text. Over three decades later, it remains indispensable for serious
On the Other Hand
As we noted above, there are many users who are very much against the C shell, and it is a good idea to understand why.
Don't miss Csh Programming Considered Harmful for an in-depth look at why many think
csh is terrible and should never be used by anybody, ever, at any time.
And if that doesn't convince you, you can read Top Ten Reasons Not to Use the C-Shell.
Should I learn the C shell?
If you are a network or systems administrator in a Unix environment, you will almost certainly run into the C shell, so it is good to at least have some familiarity with it.
Casual users and even most developers will probably be better served with another shell.
KornShell, or just
ksh, is a Unix shell that was developed by David Korn at Bell Labs in the early 1980s. KornShell is based on Bourne shell (sh), provides complete backward compatibility with sh, includes many C shell features, adds additional features that are unique to
ksh, and runs faster than either
KornShell was the default shell used with IBM's AIX operating system, the once popular but now-defunct OpenSolaris operating system. While
ksh is primarily an interactive command language, it can also be used as a high-level programming language and is useful for writing automated scripts.
ksh was released as a proprietary Unix shell for Bell Labs (and later AT&T) users. It was backward compatible with
sh and included many features borrowed from
csh at the request of Bell Labs users.
KornShell remained a proprietary AT&T shell until the year 2000 when it was released as open source software.
ksh was proprietary software from 1983 until 2000, several open source shells based on
ksh sprang up with the most popular being Public Domain Korn Shell (
pdksh) and MirBSD Korn Shell (
What Can You Do With KornShell?
KornShell scripting can be used in two different ways:
- KornShell can be used interactively to execute commands typed into a command line prompt.
- KornShell can be used programmatically to create scripts to automate a wide variety of operating system and system adiministration tasks.
Shell languages are primarily used to write code that launches other programs. However, entire scripts can also be written in shell languages such as
ksh and used to automate any repetitive administrative task. Some of the tasks most commonly tackled with
ksh or other shell languages include:
- Writing scripts that will execute when a computer system boots up
- Writing scripts that automate computer maintenance tasks
- Downloading and installing application packages.
Online KornShell Tutorials and Videos
The best online resource for programmers who are just getting started with
ksh is the Korn Shell Scripting from IBM developerWorks.
This short tutorial, which is also available in pdf, quickly introduces Unix shells, variables, loops,
ksh syntax, and much more. In the 15 minutes required to review this resource, you will learn the basics that will help you understand other more-challenging tutorials.
James Maher, an experienced Unix programmer and systems admin, released a series of 80 free KornShell tutorial videos on YouTube. These videos assume that you have some familiarity with the command line but no prior experience with
If you already have
ksh up and running on your system, these videos cover a lot of ground including variables, debugging, arrays, lists, if statements, pattern matching, loops, functions, and a lot more.
You'll want to supplement these videos with written training which provides greater depth, but these videos are an excellent way to reinforce the topics presented in written format in other resources.
Philip Brown, a Solaris systems administrator and experienced programmer at the University of Southern California, has written a free Korn Shell (
ksh) Programming tutorial. The tutorial covers topics such as variables, functions, redirection, and a lot more.
If you're up for something a little more academic, Unix Shell Scripting With KornShell/Bash is a detailed, electronic, course handout. It was created by Richard Brittain, a Research Systems Engineer in the Information Technology Services department at Dartmouth College.
Even experienced programmers refer to reference documents on a regular basis. Here is a reference resource you can use to locate detailed instructions about specific
ksh features when you need them.
- The KornShell website includes information about
ksh, links to the latest version of the software, documentation, and more.
Many programmers learn to use
ksh by studying texts written on the topic. If you do follow a text, it's important that you not try to just jump in at a point that looks interesting.
Skipping into the middle of one of these texts will lead to confusion since the text in each chapter builds on the content presented in previous chapters. Read the information presented in these texts in the order presented to get the most out of them.
- Classic Shell Scripting (2005) by Robbins and Beebe: a solid introduction to shell scripting. If
kshis your first shell programming language you would be wise to solidify your understanding of shell scripting in general before moving on to
- KornShell Programming Tutorial (1991) by Barry Rosenberg: the most highly recommended KornShell programming text available.
- Learning the Korn Shell (2002) by Robbins and Rosenblatt: part of the popular O'Reilly Media series of computer programming books. While not as popular as KornShell Programming Tutorial, this text is nonetheless very well-respected and acknowledged within the Unix programming world.
- The New KornShell Command And Programming Language (1995) by Marris I Bolsky and David G Korn: this is not for beginners. However, experienced programmers will appreciate the technical depth of this text. Plus, how can you go wrong selecting a programming langauge text written by the creator of the language?
Korn (not that KoRN) Shell is a Unix shell programming language you can use interactively to execute commands from the command line or programmatically to create scripts that can automate many computer maintenance and system administration tasks.
Bash is the most commonly used command-line interface in the Unix world. It is the default text-based interface for both Linux and Mac OS X. And it is widely used almost everywhere else.
Its popularity is due to the fact that it combines two different trends in Unix text interfaces: strong scripting ability and ease of use.
In this section, we will provide you with a basic introduction to using Bash in both these ways. In addition, we will provide resources for you to become a Bash master.
As we've discussed, the Bourne shell was the first really important Unix shell. But it had kind of a clunky user interface.
On the other hand, it had a great scripting language with a powerful yet simple syntax. At that time, Unix was primarily a programming environment, so it was assumed that users would just create their own scripts to do anything complicated.
As Unix became more widely used, people wanted shells that were easier to use interactively. Thus the development of things like the C Shell and the KornShell, which included much better user interface features.
In 1989, Brian Fox wrote Bash for the Free Software Foundation (FSF). Although it doesn't look like it, the name is actually a humorous acronym: Bourne Again SHell.
As such, it is a replacement for the Bourne shell. But it added all the interactive features that people had come to love in the other shells.
In the Bourne shell, there was not a lot you could do as a user except enter commands. Bash changed all that. Here are the big changes.
History: this allows you to find and edit previous commands. This includes the use of the exclamation mark operator (called "bang") from the
What's more, you can make changes to previous commands. It also allows the user to scroll back through previous commands and edit them directly on the command line.
Aliases: this is a kind of rudimentary programming, where you can give a complicated command (or multiple ones) to a simple alias.
Directory Stack: this allows you to "push" your current directory onto the stack while you go to another directory. Once you are done in that directory, you can "pop" off it and go right back to where you were originally working.
There is much more to using Bash, of course. The following resources should move you on your way.
Interactive Bash Online Resources
Any Bash command is, technically, a script. So there isn't as clear a line as we might like between using Bash interactively and using it for scripting.
For example, it might be very useful to use the directory stack in a Bash script. As a result, tutorials don't always make a distinction between these two aspects of Bash. But these resources focus on directly interacting with the Bash command-line.
Bash Guide: this is a really good introduction to all the basics of using Bash and getting things done. It doesn't just deal with the more advanced stuff. It includes, for example, a good discussion of filters and pipes and redirects.
Advancing in the Bash Shell: this is a great way to learn the details of using history, filename completion, aliases, and much more.
Bash Frequently Asked Questions: this is a general FAQ on Bash, with lots of information on interactive and scripting questions.
Bash is designed to be a superset of the Bourne shell. So in theory, Bash can run any Bourne shell script (and after roughly four decades, there are a lot of them). Although it isn't always true, in the vast majority of cases, it is.
Since Bash has virtually no problems with any Bourne shell script, pretty much any information about Bourne shell scripting will apply to Bash scripting.
Bash Script Example
The Bash scripting language is fairly intuitive. We'll start with a little example, and then provide some resources for you to learn more. This is a simple example that reports whether the script was run with a command-line argument or not.
#!/bin/bash if [ $# -gt 0 ] then echo "First argument: $1" else echo "There were no arguments" fi
The sharp or hashtag character (#) is generally used to start comments. But in the first line of a script, when followed by an exclamation character, it tells the shell which program should run the script.
The stuff after the exclamation mark is the complete path to the program. In the case of
bash, it is always in /bin.
Note that an older script might start with
#!/bin/sh, but on most systems, the
/bin/bash point to the same program. (On some,
/bin/sh is replaced with the simplified Bash program called Dash in
sh always start with a dollar sign. The special variable
$# contains the number of command-line arguments. The command-line arguments themselves are given numbers: $1 for the first, $2 for the second, and so on.
The variable $0 contains the program name itself. So the second line of code checks to see if there are any command-line arguments. (It could also just check if the first command-line argument,
If there are any command-line arguments, the script uses the
echo command to output what the first argument is. If not, the script prints out that there were no arguments.
Learning Bash Scripting
The Bourne shell has been in use for roughly four decades. Bash itself has been around for over 25 years. As a result, there are a lot of resources to help you learn it.
Steve's Bash Shell Scripting Tutorial: this is an excellent tutorial that features lots of examples. It's a great way to get going with Bash scripting.
Bash Programming — Introduction HOW-TO: this is a detailed introduction to Bash from the bottom up.
Linux Shell Scripting Tutorial: this not only provides an introduction to Bash scripting, but also to other tools like sed (pattern matching) and AWK (data extraction).
Writing Shell Scripts: this is the Linux Command tutorial on scripting that claims to be fun.
Unix Shell Scripting Tutorial: a ten-part video tutorial that starts at the very beginning.
Other Bash Resources
There is a whole lot to Bash. Here are some other resources that you will find useful.
Bash Reference Manual: this is the official GNU reference manual for Bash. It has all the information that you need, but it can be intimidating if you are just trying to get started.
The Bash Hackers Wiki: an extensive resource for all things Bash.
Stack Overflow: this is the site's Bash tagged threads. It's extremely active and populated by a lot of very knowledgeable people.
Bash is far too big a subject to be covered fully in a simple webpage. But using the resources found here will get you moving with Bash as a user interface and as a scripting language.
There are a lot of Unix shells to choose from. We've gone over the three most popular. If you can't decide, Bash is almost certainly the one to pick.
But some people will want something different. With the information in this article, you should be in a position to make the right choice.