Scripting Clinic: Have a Bash with This Linux Shell
If you are a Linux administrator, scripting can make your life easier by allowing you to automate routine tasks, customize jobs, and connect different utilities. We kick off our new monthly scripting column with a look at Bash, the default command shell on most Linux distributions.
Any Linux administrator who wishes to remain sane relies heavily on scripting to automate routine tasks, customize jobs, and build the plumbing that connects the different utilities that make a Linux system run smoothly. The Linux world is chock-full of scripting languages: Perl, Python, PHP, Scheme, Tcl, Tk, Ruby, Forth, Smalltalk, Eiffel, and doubtless many more. To get the column started, we'll look at shell scripting with Bash, and scripting with Python and Perl.
Understanding what Bash does is fundamental to using Linux. Bash is a command interpreter, the direct interface between you and your Linux system. Python is easy-to-learn, yet capable of handling any system administration chore. Perl is endlessly flexible, which makes learning it rather vexing at times. But it can do almost anything: Complex programs are written in Perl, such as the indispensible game Frozen-Bubble.
Scripting and text-and-file processing go hand-in-hand. So we'll also incorporate regular expressions, awk, sed, find, sort, uniq, and other text-and-file processing utilities. Half of using Linux is simply learning all these odd little specialized commands. Here is where the real power of Linux/Unix lies: All these little, specialized programs can be strung together in any way you need. You're not limited by some program author's idea of what you need, so you can use these building blocks to construct your Linux house the way you like it.
The default command shell on most Linux distributions is the Bash shell. Linux/Unix gurus adore silly wordplay; Bash stands for Bourne-again Shell. Bash is based on the Bourne Shell, the original command shell for Unix SysV systems. It incorporates features of the Korn shell, the C shell, and some new stuff of its own. Some folks love to debate command shells, and wax poetic over Csh, Tcsh, Zsh, and so forth. You are welcome to go nuts and experiment with any shells you like. Meanwhile, we're going to talk about Bash. (Should there come a time when you need to run a different shell, but don't want change your system default, simply invoke it from Bash: $ exec /bin/zsh)
For admins looking to master the shell game, the book "Linux In A Nutshell," by Ellen Siever, is indispensible. It's the best Linux command reference we've seen. It separates the built-in Bash shell commands from other commands, such as system administrator and user commands. Understanding what belongs to Bash will prevent a lot of confusion. Run this command to see what built-in commands are enabled on your system:
$ enable -a
This command displays all of your environment variables:
You should have version 2 or newer:
carla@windbag:~$ echo $BASH_VERSION
Unlike many Linux programs, Bash is mature, and does not change very often, so you don't have to worry about laboring to keep up with the latest and greatest.