Unix Programming Resources

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.

Unix Scripting

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.

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.

C/C++

Here are a few resources that should get you started as a C/C++ programmer.

Systems Programming

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.

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.

Summary

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.