Bash Programming Resources

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 most 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 article, 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.

Bash History

The Unix command-line is actually a program called a shell. One of the first to show up was the Bourne shell, generally known as simply "sh." It was written by Stephen Bourne in 1977. It had kind of a clunky user interface. But 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. This led to the popularity of things like the C shell (csh) in the 1980s. It included useful features like the command history, aliases, directory stack, and much more. And as a scripting language, it was similar to the C programming language. But it was widely criticized and never caught on.

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 C shell and the later Korn shell.

Interactive Bash

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 csh. 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.
  • Unix Glossary: this isn't specific to Bash, but it has a lot of information about Bash and other things you will run into as you get more comfortable with the shell. Be sure to check out the links on the sidebar; there's a lot of good information there too.
  • Bash Frequently Asked Questions: this is a general FAQ on Bash, with lots of information on interactive and scripting questions.

Bash Scripting

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,

Bash has no problem with any Bourne shell script. So 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/sh and /bin/bash point to the same program. (On some, /bin/sh is replaced with the simplified Bash program called Dash in /bin/dash.)

Variables 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, $1, exists.)

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.

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 Grymoire — Home for UNIX Wizards: this is Bruce Barnett's collection of tutorials for Unix tools. In particular, check out his introductions to sed and AWK.
  • 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.
  • Reddit: this is the Bash subreddit. Also check out the command-line subreddit.

Summary & Related Guides

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.

Related resources: C, C++ & C# Programming, Unix Programming, SSH & Putty -- More Programming Languages