Introduction to the bash Shell


The bash ("Bourne-again") Shell (bash) is an interactive command interpreter and programming language that has the best features of both the C Shell and the Bourne Shell. It has many of its own features as well, like built-in command line editors and unique shell functions. bash initializes a file called .bash_profile upon login to set up your environment.

Below is a brief overview on using the bash Shell. For more complete information on the bash Shell, type "man bash".

A "$" represents a command, or shell, prompt; press the Return key after each command. Text in italics is what you actually type in on the system; text in Courier are items displayed on the screen or sample code.


The bash Shell has new and improved features that make it desirable to use over most shells:

HISTORY MECHANISM: The history command provides a shortcut for users in that previous commands can be reintroduced and possibly modified. You can access these previous commands using the UP and DOWN arrows. The history file is maintained across login sessions.

COMMAND-LINE EDITING:The bash Shell has built-in Vi-like and Emacs-like editors so that you may edit the current command line and previous commands. In the simplest form, the arrow, backspace, and delete keys may be used to edit these lines.

COMMAND NAME ALIASING:Aliases provide a means of shortening the name of a command or sequence of commands. When you type an alias on the command line, the shell replaces it with the command sequence to which it refers. Aliases effectively allow you to abbreviate commands and to alter the meaning of existing commands from your point of view.

JOB CONTROL:On most systems, jobs run in the bash Shell can be stopped and moved to and from the background using the ;&and fg commands.

NEW CD CAPABILITIES: Users can return to the previous directory without typing its whole pathname. bash allows you to extend the capabilities of the cd command by replacing it with a user definition.

TILDE EXPANSION: The home directory of any user can be referred to using the tilde (~) character.


A file called .bash_history is kept by bash that stores each command you enter so that you can re-enter previous commands with ease. To display the most recent commands, type
$ history
22 cd usr/lib
23 mail
24 w
25 vi rick
26 cd
27 tin tamu.general
28 ls -al
29 history | mail
Note that each command has a number associated with it. You can limit the number of commands you want displayed on your terminal if desired:
$ history 10 # displays last 10 commands


The command:
$ !!
will re-issue the previous command. To re-execute command 13,
$ !13
To execute the last command that started with a "c ",
$ !c


Aliasing allows you to redefine the commands used by UNIX. Aliases are usually performed only on the first word of a command. If the command name matches an alias, the command is replaced by the text of the alias. If the last character of the replacing text is a blank, the word following it is also checked for an alias.


Aliases can be set on the command line or in your .bash_profile using this syntax:

$ alias command='text'
Here are some example aliases:
alias h='history'
alias copy='/bin/cp -i'
alias a='alias'
a dir='ls -alF'
a cls='clear'
a mail='pine'
a rn='trn -XX -x61s'


One interesting feature of the cd command in bash, "cd -," allows you to return to the previous directory without much typing. Here's an example:
$ pwd
$ cd /tmp1/model; pwd
$ cd; pwd
$ cd -; pwd
"cd -" simply changes the current working directory to the previous working directory. "cd oldstring newstring" substitutes "oldstring" with "newstring" in the working directory name. To print the current directory in your prompt, insert the following in your .profile:
export PS1='${PWD}$ '


A tilde (~) refers to your home directory when not attached to a userid, and "~abc1234" is a quick way to reference abc1234's home directory. Examples:
$ cd ~/mydir # change to your "mydir" directory
$ cd ~abc1234/herdir # change to abc1234's "herdir"
Reference: Newham, C. and Rosenblatt, B., Learning the bash Shell, 1995.

Last Modified: 05:39pm CDT, September 10, 2000