The Bourne Again Shell

Back Up Next

horizontal rule

Chapter 1
Chapter 2
Chapter 3
The Shell
Parameters and Variables
The Bourne Again Shell
Grep and AWK
Control Structures
Shell Program examples
TC Shell
Programming tools
Networking and the Internet

The Bourne Again Shell


BASH (Bourne Again Shell) - It is a command interpreter and a high level programming language.

It was based on the original Bourne shell (sh) written by Steve Bourne of AT&T


You can:

Create shell scripts
Change prompt
Create aliases
Put programs to run on background
Modify values of variables


Bash reference manual


(Portable Operating System Interface for UNIX) An IEEE 1003.1 standard that defines the language interface between application programs and the LINIX operating system. Adherence to the standard ensures compatibility when programs are moved from one UNIX computer to another. POSIX is primarily composed of features from UNIX System V and BSD UNIX.



A shell script is a file that contains commands that can be executed by the shell. The shell can be any command or group of commands that enter at the shell prompt.


sample example


Control structure (alters the execution of the flow of the program)

Make the file executable

Use chmod 777 or other permission can be used to make the file executable.

Then to run it use use full path to indicate that program is in current dir. I.E. ./myscript -/myscript





; - command separator

I.E. $progl ;prog2;prog3


\ - if command is long you can use \ to continue command in the next line.


|- pipe

I.E. $ls-l | grep tmp


& - run on background task symbol

I.E. progl & prog2 & prog3

Combination of them

$ progl | prog2 | prog3 &



A command can send messages to standard error so it does not get mixed up with the information that it is sent by the command to the standard output.


When program runs, the process running the program opens three file

descriptors (places the program sends its output to and gets its input from: 0 (standard input) 1 (standard output) 2 (standard error)


>short for 1 > (redirect standard output)

<short for <0

2> redirects standard error

2>& 1 Declares file descriptor 2 to be a duplicate of file descriptor 1


I.E. cat x y 1> outputl 2> output2

If y does not exists the content of output2 will be the error message


$cat x y 1> hold 2>&1

the standard output and standard error are both redirected to hold




A job is a command pipeline. You can create several jobs concurrently without interfering your work.


You can move commands from foreground to background and vice versa.



$find /usr -name ace -print > findout &

(it creates a job # [2]=second job)

[2] 1234


$fg %2 (brings jog to foreground)


$fg %find


$fg %f


$fg %?ace


Suspending a job

CTRL-Z or CTRL-Y will suspend a job

$bg (it will resume its execution)


$ (sleep 5; cat > mytest) & (sleep - delay for a specified amount of time)

[1] 1456

$ date

[1]+ Stopped (tty input) (sleep 5; cat > mytest)


(sleep 5; cat > mytest)

I had to bring this job forward to complete it since the job attempted to read from the terminal






In bash you can store a list of directories that you are working with. This is referred to as a stack.

$dirs (it will display the stack)

$pushd ../ (will place on the stack my previous dir)

$pushd /home (it will place on the stack the dir /home)

$pushd /home/ftp (it will place on the stach the dir /ftp)


When you use the pushd dirname, the dirname will be the active(current) directory

When you use pushd by itself the next directory on the stack it will go to the top of the stack


$pushd +2 (+0,+1 first dir on the stack represented by 0)


$popd (it will remove the top dir from the stack)

$popd +1 (it will remove the second dirs from the stack)




$ps gives a snapshot of the current processes.


The process structure is hierarchical, it has parents, children, and even a root. A parent process forks a child process, which in turn can fork other processes.


[wmorales@rc33uxas01 wmorales]$ ps -l


100 S 501 8772 8771 0 76 0 - 434 wait4 pts/2 00:00:00 bash

000 R 501 8919 8772 0 79 0 - 617 - pts/2 00:00:00 ps


F- Flags associated with the process

UID - User id number

PID - process identification number

PPID - The process identification number of the parent process

PRI - Priority

WCHAN - wait channel. If process is wating for some even this column gives the address of the kernel functin that cuased the process to wait.

SZ - Size

TTY - Terminal name controlling the process

TIME - The number of minutes and second the process has been running

CMD - Command


Executing a command

Builtins Programs that are built into shell


bulletWhen you give a shell command , it usually forks a child process to execute the command.
bulletThe parent process goes into sleep.
bulletWhen child process is finished, parent wakes up.


Calling a shell script


Giving a shell command, the shell forks (creates a child process that differs from the parent process) creating a duplicate of the shell process (a subshell). The new process tries to exec the command.


$bash whoison or $sh whoison

Specifying a shell

$cat bash_script

echo "This is a Bourne again shell script."


$cat tcsh_script

echo "This is a tcsh script."
set person = jenny
echo "$person"

# without the != comment

Statup files


If bash is the login shell

  1. it reads /etc/profile looking for commands
  2. .bashrc (if shell finds it) or
  3. .bash_login (if shell finds it) or if neither exist
  4. .profile

if you get into bash just typing bash it will read

  1. .bashrc The shell inherits any environement variables (exported) from varibles in the parent shell os the /etc/profile and .bash_profile are passed in


When exiting the shell reads the .bash_logout in your home directory.



horizontal rule

Back to CS140U Homepage
This page was last modified September 26, 2004