Moving to Bash as Your Shell



Last updated on March 15th 2001


Contents

Introduction
Why bash?
Getting bash
Installing bash
Using bash as Your Shell
Important Links

Introduction

The bash shell, like other shells - is a command interpreter that allows you to interact with your system. It's quickly gaining popularity amongst the available shells and is the default for operating systems like Linux and the *BSDs.

Why would you want to switch? The bash shell offers enhanced features over most other shells and because of it's cross-platform portability means that you could have it running on a variety of different platforms and systems and all your shell scripts will also therefore be portable - and not subject to the whims of various operating system vendors, their choice or implementation of the shell.

This article aims to show you what bash is about and how to get it onto your system. There will be an opportunity for you to try it out and see what you think - and if you like it, how to make it your default shell.

Why bash?

Simply described, bash is a shell, much like tcsh, ksh or csh among others and is often how you interact with a Unix system via the command-line (CLI). It reads your input and translates them into actions. While it is available for Windows as well, bash is strictly a command-line driven application.

What exactly is a shell, you ask? What a shell is, is best described in the bash reference manual as follows:

"At its base, a shell is simply a macro processor that executes commands. A Unix shell is both a command interpreter, which provides the user interface to the rich set of Unix utilities, and a programming language, allowing these utilitites to be combined. Files containing commands can be created, and become commands themselves. These new commands have the same status as system commands in directories like `/bin', allowing users or groups to establish custom environments.

A shell allows execution of Unix commands, both synchronously and asynchronously. The shell waits for synchronous commands to complete before accepting more input; asynchronous commands continue to execute in parallel with the shell while it reads and executes additional commands. The redirection constructs permit fine-grained control of the input and output of those commands, and the shell allows control over the contents of their environment. Unix shells also provide a small set of built-in commands (builtins) implementing functionality impossible (e.g., cd, break, continue, and exec), or inconvenient (history, getopts, kill, or pwd, for example) to obtain via separate utilities. Shells may be used interactively or non-interactively: they accept input typed from the keyboard or from a file. All of the shell builtins are described in subsequent sections.

While executing commands is essential, most of the power (and complexity) of shells is due to their embedded programming languages. Like any high-level language, the shell provides variables, flow control constructs, quoting, and functions.

Shells have begun offering features geared specifically for interactive use rather than to augment the programming language. These interactive features include job control, command line editing, history and aliases."

Okay, so that explains what exactly a "shell" is, but what exactly is bash and how does it differ from other shells? The name "bash" is actually from "Bourne Again SHell" and implies that it is a modified version of the "Bourne shell." From the bash home page, bash is described as:

"Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use. In addition, most sh scripts can be run by Bash without modification."

That's quite a mouthful, but it still doesn't explain why you would want to use bash over the other shells that are available - including the ones that come with Solaris. The standard shell for Solaris is the "Bourne shell" or /usr/bin/sh - but it is referenced as /bin/sh (and in the case of root, /sbin/sh). While it does what it's supposed to and gets the job done, as far as features go it could be better. Some of the enhanced features of bash include:

While the list above doesn't look impressive, once you start using bash you will appreciate it's power. The command line editing feature is excellent, and allows you to manipulate your command-line using standard vi or emacs keystrokes (which you can specify). The command history is also an excellent feature as you can perform "bang commands" repeatedly, saving you a lot of typing (via the "!###" syntax).

The functions and aliases features allow you to not only program bash scripts that are powerful but you can also extend bash with existing commands - or your own. This is what was meant by, "files containing commands can be created, and become commands themselves" above.

Job control is another neat feature, allow you to run commands in the background, foreground - or switch them on-the-fly as you work within the shell.

The bash shell is quickly gaining ground as the preferred shell, certainly amongst the free Unix crowd such as Linux and the *BSDs. I happen to use it on all of my systems, be they running Linux, Solaris or Irix. It's portable, and ensures consistency from one platform to the next. Also, once you start using powerful features like the command-line editing, aliases and job control - you'll be hard pressed to return to your former shell.

Let's now discuss how to obtain and install bash for your Solaris system, try it out - and if you like it, make it your permanent shell.

Getting bash

The quickest and easiest way to get bash and start using it immediately is to get it in a Solaris package format. This allows you to download one file, install it - and go to work. Later, if you decided to remove it, you can do so with one command. You can also get the source code for a different version of bash and compile it yourself should you want to do that.

For the scope of this article, we'll just go with a simple binary package install as this will be sufficient for almost all situations.

The first step is to obtain the bash package, and one place that comes to mind is Sunfreeware.com as it has the packages available for several versions of Solaris and platforms. Hit that site and look on the top right pane, where you'll see several versions and platforms of Solaris listed - pick yours. On the lowermost right pane you'll see a list of software packages. Scroll down and select "bash-2.03" (available for all platforms listed, save for Solaris v2.5 and v2.6). On the center pane you will now see the entry for bash called "bash-2.03-sol7-sparc-local.gz" or similar, depending on the platform and version of Solaris you have chosen. Using Netscape Communicator for example, you'll want to shift-click to download that link's package.

You can also get the package by just clicking one of the following links here, which link to the package itself on Sunfreeware.com (remember to shift-click the link to download):

More versions are available on the site, so you should check there if your platform isn't listed above.

Installing bash

Following the download procedures above, you should be left with a package named accordingly to your platform - and ending with ".local" in the filename. In order to install this package, you should be root or have root access as it installs in your /usr/local filesystem - or creates it if you don't have one. So, become root somehow by logging in as root, using the su or sudo commands or however your system is set up.

Next, issue the pkgadd (PacKaGe ADD or "package add") command to install it on the system, changing the actual package name to fit the one you downloaded, for example:

# pkgadd -d bash-2.03-sol7-sparc-local (hit return)

You will then be presented with the standard pkgadd response:

The following packages are available:
  1  GNUbash    bash
                (sparc) 2.03

Select package(s) you wish to process (or 'all' to process
all packages). (default: all) [?,??,q]:

...to which you'll want to answer with an "all" or just hit return, since that is the default value. You'll see the files being installed start to scroll by on your screen and the package will be installed! Yes, it's that simple - and why we like packages, right?

You may be presented with a few questions along the way if you haven't installed any other packages in this fashion before, including the creation of the /usr/local directory or perhaps the permissions settings on various "conflicting" files. Just answer "y" to these questions and hit return.

Once the package has completed installing, you'll be returned to the command prompt after a brief line that reads, hopefully:

.
.
.
Installation of <GNUbash> was successful.
# _

You can verify this and see some information about the package with the pkginfo (PacKaGe INFOrmation or "package information") command:

# pkginfo -l GNUbash (hit return)
   PKGINST:  GNUbash
      NAME:  bash
  CATEGORY:  application
      ARCH:  sparc
   VERSION:  2.03
   BASEDIR:  /usr/local
    VENDOR:  Free Software Foundation
    PSTAMP:  Steve Christensen
  INSTDATE:  Aug 17 2000 05:18
     EMAIL:  steve@smc.vnet.net
    STATUS:  completely installed
     FILES:     35 installed pathnames
                 6 shared pathnames
                 7 directories
                 3 executables
              9341 blocks used (approx)
# _

Yours may vary slightly, but should more or less look like the above. You're all set, and should now log out as root and "be yourself" again.

Using bash as Your Shell

Bedore you commit to making bash your default shell, you might want to try it out first and see what it's all about. The package installed above installs a binary under /usr/local/bin simply called "bash." To exectute it, you could issue this command at the command prompt:

$ /usr/local/bin/bash (hit return)
$ _

Hmmm. That doesn't look very different, now does it? Recall from above where bash is described as "a sh-compatible shell" - therefore, it looks the same upon initial inspection. You can customize your command prompt any number of ways and even use ANSI colors to return the date, time and/or location within the filesystem - among other possibilities.

Try out some of your normal commands and work on the system as you normally do. Seems to be the same, right? Now try hitting the "up" or "down" arrow keys and you'll start to see the power of bash - specifically, it's command history. Try typing in history at the command-line and you'll see a numbered list of the commands you've used. You can re-run a previously issued command with a "bang" by simply examining your command history, noting the number before it - and issuing "!###" at the command prompt - where "###" is the number of the command-line you wish to re-run.

Let's try editing a command-line quickly. Hit the up arrow to bring up one of your previous commands. Now try using the "left" and "right" arrow keys to move around on the line. You can erase characters, insert new ones, move to the beginning of the line with "Ctrl-A" and the end with "Ctrl-E" depending on how your preferences are set (emacs/vi). There's a lot more power there, and this is just a sample.

Let's try out the job control features. Start up something simple like xcalc but don't run it in the background with the ampersand ("&") suffix. Normally, this would lock your terminal until you had exited xcalc. With bash you could simply issue a "Ctrl-Z" to suspend xcalc and return to it later by issuing a fg command. What if you wanted to keep running xcalc in the background though? Rather than issuing the fg command, use bg instead!

WARNING! Do not change the default shell for root unless you understand that you may have problems later on should you need to boot into single user mode or hose your libraries for some reason. The shell for root, /sbin/sh is "statically linked" meaning that that binary does not rely on outside system libraries which could be on external partitions that might fail to mount. In effect, locking you out. However, if you have one large filesystem, save for maybe /var and /tmp for example, then this isn't much of an issue, because / and /usr and even /usr/local will be on the same filesystem. If you can't mount root, then you have other problems to worry about. I'll link to a few other articles on this subject here shortly...

Okay, so you're convinced that bash is for you. How do you make it your default shell? As root, you must edit the /etc/passwd file - and replace the "/bin/sh" part with "/usr/local/bin/bash" instead. That's it! You should make sure that the change works before actually logging out though, or you might not be able to log back in again. Try telnetting to your box in a terminal and logging in as yourself after the above change has been made. You should be able to get in okay, but this time your shell is bash by default! If this is the case, you might want to exit the Common Desktop Environment (CDE) or OpenWindows - or at the console, if that's where you're at - and log back in again to start using bash now. You're done!

Where to go from here? Start reading the bash reference manual linked below and play around with the various features. Get to know the shell and try out some scripts of your own. Enjoy!

Important Links

Bash home page
The Bash reference manual


Content and images are copyright 2001 by Michael Holve