blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

Shell prompt

This is a cursor that blinks when a terminal window is opened. It shows the position of where the next command/text would be typed. A general format of a shell prompt is username @ machinename : currentworkingdir $ if it's a “#” instead of “$” then that user has superuser privileges.

Command history

As such a terminal does not allows a user to go back and edit the previous command. Instead a user could make use of a feature called “command history” using which he could bring any previous commands to the prompt (using arrow keys) and then edit them as needed. Most Linux distributions remember the last five hundred commands by default.

Virtual terminal

Even if there are no terminals to be seen there are terminals running behind the GUI(Especially if you are using Ubuntu or any GUI enveloped Linux systems). These terminals could be accessed using Ctrl+Alt+F1 through Ctrl+Alt+F6. To switch back to GUI use Ctrl+Alt+F7.

Is shell and terminal the same?

The Wiki defines shell as "A shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel." There couldn't be a better definition. All modern operating systems provide a shell so that users could harness the power of the operating system to it's full. Now terminal refers to terminal emulator which is a software that opens a window and lets you interact with the shell. There are a bunch of different terminal emulators you can use. Most Linux distributions supply several, such as: xterm - the standard terminal emulator for the X Window System. konsole - KDE. gnome-terminal - Genome

Customizing the shell prompt

The prompt is defined by an environment variable named PS1 (short for “prompt string one”). We can view the contents of PS1 with the echo command:

[On Ubuntu 10.10]

eipe@eipe-john:~$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+$debian_chroot)}\u@\h:\w\$

Most of the backslash characters that we see above are escape sequences. They are discussed under the topic escape character sequences.

There are other variables PS2, PS3 and PS4 which are infrequently used by an average user. These are discussed in environment variables tutorial. There is another variable PROMPT_COMMAND. The contents of this variable are executed as a regular shell command just before a shell displays a prompt.

Now, let's modify the PS1 variable to change the appearance of the prompt. But before you do that take a back up of the variable. It is easily done by assigning this to a new variable.

$ ps1original=”$PS1”
$ echo $ps1original

Character color is controlled by sending the terminal an ANSI escape code embedded in the stream of characters to be displayed. These escape code does not get printed on the display, rather it is interpreted by the terminal as an instruction. The \[\033 and \] escape sequences are used to encapsulate non-printing characters.
For colour escape sequences, they should also be followed by a lowercase m. More on sequences in escape sequences tutorial. Common color codes:

Black0;30Dark Gray1;30
Blue0;34Light Blue1;34
Green0;32Light Green1;32
Cyan0;36Light Cyan1;36
Red0;31Light Red1;31
Purple0;35Light Purple1;35
Light Gray0;37White1;37

The simplest prompt would be a single character, such as

eipe@eipe-john:~$ PS1=$

A more complex styling would be,

eipe@eipe-john:~$ PS1='\[\033[1;36m\][\u:\s \v]\T \w\$:\[\033[0;30m\]'

[eipe:bash 4.1] 10:43:29 ~$:

The reason why \033[0;30m is given in the end is to revert back to another color (in this case black) so that the text which the user types is not in Light Cyan color. \u prints the current user, \s the type of shell and \v it's version. \T prints the current time in 12hr format and \w prints the current working directory. It's also possible to set the text background color using the codes listed below.

0;43mBrown0;47mLight Grey

There are lot of other tweaks that one can do using the PS variable. It includes moving the cursor, bold, underscore, blink, etc. To save the value permanently by typing the command,

$export PS1

This will create an entry in .bashrc file. PS1 is the default interaction prompt. Lets see the other prompts that are available.

PS2 - Continuation interactive prompt

A very long unix command can be broken down to multiple line by giving \ at the end of the line. The default interactive prompt for a multi-line command is ">". Let us change this default to display "->" by using PS2 environment variable as shown below.

eipe@eipe-john:~$ echo $PS2
eipe@eipe-john:~$ PS2="->"
eipe@eipe-john:~$ echo $PS2
eipe@eipe-john:~$ ls \

PS3 – Prompt used by “select” inside shell scripts

The default is "#?" for select command prompt. Consider a simple shell program
$ vi
type in the below program

select i in 1 2 3 exit do
case $i in
1) echo "First";;
2) echo "Second";;
3) echo "Third";;
exit) exit;;
esac done

$ chmod 755
$ ./
1) 1
2) 2
3) 3
4) exit
#? 1 First
#? 4

Now to change the default #? prompt. Add the below line to the top of the script.
PS3="Select a num: "

1) 1
2) 2
3) 3
4) exit
select a num:4

PS4 – Used by "set -x" to prefix tracing output

The PS4 variable defines the prompt that gets displayed, when you execute a shell script in debug mode as shown below. Type in the below shell script into a file

set -x echo "the number of png files in home" ls /home/eipe/Documents/* | grep "png" | wc -l

eipe@eipe-john:~$ ./
++ echo 'the number of png files in home' the number of png files in home
++ wc -l
++ grep png
++ ls 4

To change the default to something else use the below line above the sh file.

$LINENO is a variable that stores the current line number.

eipe@eipe-john:~$ ./
11@ echo 'the number of png files in home' the number of png files in home
11@ wc -l
11@ ls
11@ grep png 4


The contents of this variable are executed as a regular Bash command just before Bash displays a prompt.

eipe@eipe-john:~$ PROMPT_COMMAND="date"
Sat Jan 29 10:23:33 IST 2011
Sat Jan 29 10:23:33 IST 2011

Advanced prompt editing

You can use the output of regular Linux commands directly in the prompt as well. Obviously, we don't want to insert a lot of material, or it will create a large prompt.Linux comes with a lot of small utility programs like date, grep, or wc that allow you to manipulate data.
If you find yourself trying to create complex combinations of these programs within a prompt, it may be easier to make an alias, function, or shell script of your own, and call it from the prompt. We will see this in separate tutorials

No comments:

Post a Comment