Unix Programming: Geeks Love It. Here’s What It Can Do For You.
In the old days, Unix was an arcane subject. Most computer users stayed away from it because it was hard to use. But with the rise of the Gnu Project and Linux, it has become of comparable user friendliness to Windows and the Mac.
In fact, Mac OS X is a Unix operating system, and for many hardcore users, it is the text-base Unix interface that they use to do serious administrative tasks. As a result of all of this, Unix is more popular and important today than it ever has been.
But it is wrong to think of Unix as just the old command-line interface. The breadth of Unix programming is great. It spans a range from administrative scripting to text based coding to X Window development. And all of these kinds of programming have their uses. In this resource page, we will look at all of these approaches and how they can help you accomplish your goals.
When you are using the Unix command-line, you are actually using a program called a shell. From the late 1970s onward, the default was the Bourne shell, generally known as simply “sh.”
It was not the nicest interface to use. But for scripting, it was extremely powerful with a simple syntax. The point of it was to create scripts that could be run to do anything that needed to be done. But since not all Unix users were programmers, other shells were created. For example, the C shell (csh) became very popular in the 1980s and the Korn shell (ksh) later still.
But in 1989, Brian Fox wrote Bash. It was created to replace the Bourne shell. But it added to it most of the extra features that the C shell and Korn shell offered. It has become something of the standard shell. It is the default shell used for Linux and Mac OS X.
But what is most important is that it is syntactically the same as the Bourne shell, and so can run all the same scripts. Although there are C shell scripts and Korn shell scripts, most all shell scripting is done with good ol’ sh.
Shell Script Example
The Bourne shell 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/sh if [ "$1" ] then echo "Argument: $1" else echo "No arguments were provided" fi
The sharp or hashtag character (#) is 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.
Another common scripting language is Perl, and if you were writing a script with it, it would start with the line
#!/usr/local/bin/perl. The stuff after the exclamation mark is the complete path to the program. In the case of sh, it is always in /bin. Perl could be in a number of locations, but in this example, it is /usr/local/bin.
Variables in sh always start with a dollar sign. Command-line arguments 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 the $1 variable exists.
If it does, the script uses the
echo command to output what that argument is. If not, the script prints out that no argument was found.
Learning Shell Scripting
People have been using the Bourne shell for a very long time, so there are a lot of resources for learning it.
- Linux Shell Scripting Tutorial: this is a good introduction not just to Bourne (or Bash) scripting, but also to more advanced tools like sed (pattern matching) and AWK (data extraction and formatting).
- Writing Shell Scripts: this is the Linux Command tutorial on scripting that promises, “Here Is Where The Fun Begins.” It certainly can be fun to learn to do amazing things with shell scripting.
- Steve’s Bourne / Bash Shell Scripting Tutorial: this is Steve Parker’s excellent tutorial with lots of great examples. It’s a simple way to get started.
- Unix Shell Scripting Tutorial: a ten-part video tutorial that starts at the very beginning.
Other Shell Scripting Resources
There’s a lot more to Unix shell scripting than the Bourne shell itself. Here are some other things that you might find useful.
- Bash Frequently Asked Questions: this is a general FAQ on Bash, but it has lots of information about problems you will run into while scripting.
- Bourne Shell Reference: a one page reference on those little scripting details that you forget from time to time after you know how to script.
- The C Shell Tutorial: this is a very basic introduction to the C shell. But check out Top Ten Reasons Not to Use the C Shell. Or just take the advice of experienced coders and don’t use the C shell for scripting.
- The Grymoire — Home for UNIX Wizards: this is Bruce Barnett’s amazing collection of tutorials for Unix tools. In particular, it includes introductions to sed and AWK. In addition to a lot of great information, Barnett is a very amusing writer.
- Beginner’s Introduction to Perl: this is a good way to get started with Perl, if you wish to branch out into it. Also: check out our Perl Resources page.
Unix Systems Programming
In these days of graphical user interfaces, people often forget about systems programming, but there is still much call for it. You might use it for programs that just don’t have much (or any) user interface, for creating background processes or daemons, or even adding to the operating system itself. Unix was developed as a software development platform, where programs were created for various other platforms. So it isn’t surprising that it continues to be a very popular platform for programmers.
Early on in its development, Unix was rewritten in the C programming language. As a result, Unix has always been closely tied to C and then later C++. Most other languages are available on Unix, but systems programming is still primarily a C/C++ kind of thing. As a result, if you want to be a Unix systems programmer, you should know these languages.
Here are a few resources that should get you started as a C/C++ programmer.
- C Developer Resources: our own resource page to help you as a C programmer.
- C++ Developer Resources: our resource page for C++ programmers.
- A Quick Introduction to C++ (pdf): a good article that runs through all the basics of the language, including classes.
In general, Unix systems programming is something you learn from a book. It is a broad and deep subject, and so usually requires more than a quick tutorial.
- Unix Systems Programming in a Nutshell (pdf): this is a document from Northwestern University that goes over the basics of systems programming with some C examples.
- UNIX Systems Programming: Communication, Concurrency and Threads: this is a textbook on the subject that covers pretty much everything. There is also a resources page for it that contains links to all the examples in the book.
- The Linux Programming Interface: A Linux and UNIX System Programming Handbook: this is a great and exhaustive introduction to the subject with an emphasis on Linux.
- UNIX System Programming for System VR4: this is an old O’Reilly book focused on an old version of Unix, but still very useful.
- The Design of the UNIX Operating System: for those who really want to get into Unix system programming, this is the Bible. It’s not a programming book, per se, but a thorough introduction to how the operating system works.
X Window System
The final major piece of Unix programming is the X Window system. It is the graphical user interface built on top of Unix. It is truly vast. But here are some resources to get you started with the system.
- X Window Programming/Xlib: this is a very short introduction to the structure of the programming interface. X Window Programming is layered, and this explains that.
- A Brief Intro to X11 Programming: a basic introduction to X Window programming.
- Xt and the Athena Widgets: this is a basic introduction to the X Toolkit Intrinsics (Xt) using the athena widgets.
- The Road to X/Motif: a detailed introduction to programming the Motif window manager.
There is no way to fully cover Unix programming in a single page. People can spend their whole lives studying the system — many, in fact, have. But these resources will get you moving in the right direction. And with Unix being more popular than ever, it’s a great time to become a master of Unix programming.