bash – GNU Bourne-Again SHell
Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file. Bash also incorporates useful features from the Korn and C shells (ksh and csh).

The Shell Defined
Whenever you login to a Unix system you are placed in a program called the shell. You can see its prompt at the bottom left of your screen. To get your work done, you enter commands at this prompt.

The shell acts as a command interpreter; it takes each command and passes it to the operating system kernel to be acted upon. It then s the results of this operation on your screen.

Command Prompts
Command Prompts in Bash are controlled using the following environment variables:


A variable holding a command to be executed just before the primary prompt, $PS1 is to be ed.

This is the main prompt, seen at the command line.


The secondary prompt, seen when additional input is expected. It s as “>”.


The tertiary prompt, ed in a select loop (see Example 10-29).


The quartenary prompt, shown at the beginning of each line of output when invoking a script with the -x option. It s as “+”.

The PS1 variable is your primary prompt. Depending upon how your system is configured, the PS1 variable will vary. PS1 is normally defined in /etc/profile, but can be overridden by defining it again in ~/.bash_profile.

bash recognizes special characters prefixed with a backslash for the PS1 variable. These characters are:

t the current time in HH:MM:SS format
d the date in “Weekday Month Date” format (eg, “Tue May 26″)
n newline
s the name of the shell
w the current working directory
W the basename of the current working directory
u the username of the current user
h the hostname
# the command number of this command
! the history number of this command
$ if the effective UID is 0, a #, otherwise a $

These are the characters that allow you to change your prompt. If your PS1 variable is set as PS1=”[u@h W]$”, then your prompt will look like this:

[xconsole@localhost /etc]$

If you were to change it to the following: PS1=”[t s]$ “, you would get:

[12:18:24 bash]$

In addition to these special backslash characters, you can use commands. For instance, to have your prompt run as a fortune, you can do this:

PS1=”`fortune` $ ”

Notice that you have to use “`” instead of “‘”. This changes PS1 to the following:

He is no lawyer who cannot take two sides. $

As you can see, bash is very lenient about the way you configure it, so knock yourself out. The PS2 variable is your secondary prompt and is used when you have typed an incomplete command, or when you have typed a backslash at the end of a command [TRANSLATION: A backslash at the end of a command in bash tells it that you are not done with the command. This is when it will present you with the PS2 variable. bash is also smart enough to know when the command you are typing is incomplete, and in such a case, it will present you with the PS2 variable]. When you are presented with the PS2 variable, bash expects you to finish the command before it will attempt to run it. To see your current PS2 variable, run the following command:

xconsole$ if [ -f /etc/profile ]; then

When you press ENTER, you will see that you have a new prompt:

xconsole$ if [ -f /etc/profile ]; then

This prompt is the PS2 variable. You can also view it with echo $PS2. In the above example with the if statement, we did not add a backslash character right at the end of the command, but bash knew the command was incomplete. As with PS1, it is defined in /etc/profile, and can be overridden and redefined in ~/.bash_profile. It recognizes the special backslashed characters, as well as other programs like fortune [TRANSLATION: Whatever applies to PS1, applies to PS2 as well].
Created 2003-02-27 12:00:00 by converted
Updated 2005-07-08 19:07:24 by robertjw
Debug Scripts
When things don’t go according to plan, you need to determine what exactly causes the script to fail.

Bash provides extensive debugging features. The most common is to start up the subshell with the -x option, which will run the entire script in debug mode. Traces of each command plus its arguments are printed to standard output after the commands have been expanded but before they are executed.

How to get bash to clean the screen after logging out?
echo “clear” >> .bash_logout

Find recently modified files
If you ever need to find files modified within past day and print them to a file try this from a bash prompt.

find ./ -mtime -1 ! -type d -print > ./changed.txt

The -1 after mtime is the number of days, in this case, within the last 24 hours.

The ! -type d part tells the find command to ignore directories.

The -print part just means to print the path and file name on the standard output.

The > ./changed.txt part directs the standard output to a file called changed.txt. If the file already exists, it will be overwritten.

Leave a Reply

Your email address will not be published. Required fields are marked *