[go: up one dir, main page]

0% found this document useful (0 votes)
35 views6 pages

Bash Prompt Basics

This document discusses customizing the bash shell prompt in Linux. It covers the default bash prompt, bash prompt variables like PS1 and PS2, and special characters that can be used in the prompt like \u for username and \h for hostname. It also explains how to define prompts permanently in initialization files like ~/.bashrc versus temporarily on the command line. Examples are given of prompts that display the time, count files in the directory, and use colors.

Uploaded by

logion78839
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views6 pages

Bash Prompt Basics

This document discusses customizing the bash shell prompt in Linux. It covers the default bash prompt, bash prompt variables like PS1 and PS2, and special characters that can be used in the prompt like \u for username and \h for hostname. It also explains how to define prompts permanently in initialization files like ~/.bashrc versus temporarily on the command line. Examples are given of prompts that display the time, count files in the directory, and use colors.

Uploaded by

logion78839
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Bash prompt basics

linuxconfig.org/bash-prompt-basics

admin

Introduction
When managing a linux/unix operating system from the command line, users are
interacting with the system via shell. This article will explore some of the basic features
of the bash shell prompt. Default bash command line prompt on many linux systems
does not have a color and display information about user's username, hostname and
current working directory. As you well see in the following sections of this document this
default settings can be easily changed by exporting a bash prompt PS{n} variables. Bash
prompt can be modified to suit users needs and can display time, load, number of users
using the system, uptime and more.

Bash prompt variables


As anything else in the linux system also bash prompt can be customized. Bash prompt
can be customized by changing the values of bash PS1, PS2, PS3, PS4 variables. To keep
the things simple, this article will be concerned just with PS1 and PS2 variables. Use echo
command to see the current value of PS1 and PS2 variables.

echo "Bash PS1 variable:" $PS1


echo "Bash PS2 variable:" $PS2

SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and
tutorials.

Bash PS1 prompt variable


PS1 is a primary prompt variable. Currently it holds \u@\h:\w\$ special bash characters.
This is the default structure of the bash prompt on many Linux systems and is displayed
every time you log in using a terminal.Please see the following section "Bash prompt
special characters" for explanation of \u, \h, \w and \$ symbols. Here is a classical bash
prompt with default settings:

1/6
Bash PS2 prompt variable
PS2 bash shell variable is a secondary prompt. This prompt is displayed for if the shell
waits for a user input, for example your forget to insert second quotation.

Bash prompt special characters


Bash prompt can be customized by use of special characters. This table contains bash
prompt special characters and its explanation.

Bash Bash
special Bash special character special
character explanation character Bash special character explanation

\a an ASCII bell character \d the date in "Weekday Month Date"


(07) format (e.g., "Tue May 26")

\] end a sequence of non- \e an ASCII escape character (033)


printing characters

\h the hostname up to the \H the hostname


first `.'

\j the number of jobs \l the basename of the shell's terminal


currently managed by the device name
shell

\n newline \r carriage return

\s the name of the shell, the \t the current time in 24-hour


basename of $0 (the HH:MM:SS format
portion following the final
slash)

\T the current time in 12- \@ the current time in 12-hour am/pm


hour HH:MM:SS format format

\A the current time in 24- \u the username of the current user


hour HH:MM format

\v the version of bash (e.g., \V the release of bash, version +


2.00) patchelvel (e.g., 2.00.0)

\w the current working \W the basename of the current working


directory directory
2/6
\! the history number of this \# the command number of this
command command

\$ if the effective UID is 0, a \nnn the character corresponding to the


#, otherwise a $ octal number nnn

\\ a backslash \[ begin a sequence of non-printing


characters, which could be used to
embed a terminal control sequence
into the prompt

\D{format} the format is passed to strftime(3) and the result is inserted into the prompt
string; an empty format results in a locale-specific time representation. The
braces are required

Bash prompt customization


After user login to the systems user environment variables initialized from various files
like:

global system files /etc/profile or /etc/bashrc


user files ~/.bash_profile , ~/.bash_login , ~/.profile , ~/.bashrc or ~/.bash_logout

It is important to know that all users environment variable have a life time equal to the
terminal session. When the terminal session is closed the user's variables including bash
shell variables defined during a terminal session are emptied and a again redefined
when new terminal session is created either via logo in shell or interactive shell. Lets
define two variables to prove this statement.

Permanent bash variable definition


First permanent variable we define in one of the bash initialization files ~/.bash_profile
and second variable we define on the shell prompt. Let's define permanent user
variable:

3/6
What happened here, is that user "prompt" modified its own .bash_profile initialization
file located in his/her home directory by appending a VAR1 variable definition. When
user "prompt" logged out and logged in again the $var1 variable is initialized and
available for the new terminal session.
On the same principles we can define our bash prompt. The best place to do it is that
bash initialization file .~/bashrc. open up your ~/.bashrc file and add/edit the line
defining a PS1 variable to something like:

PS1='MY NEW BASH PROMPT@\t:\w$ '

NOTE: Your ~/.barshrc file may differ from the example below !

After the user "prompt" restarts a terminal session he/she is welcomed by new bash
prompt. This changes to the bash prompt are permanent.
HINT: Use "source" command to re-read a bash initialization .bashrc file instead of
restarting a terminal session.

source .bashrc

or similarly

. .bashrc

Temporary bash variable definition


Temporary bash variable definition is defined only for a lifetime of the current terminal
session. This is tome by an export command.

4/6
As you can see the variable $VAR2 is not defined when user closes his/her terminal
session. The permanent variable $VAR1 is always defined from the bash initialization file:
~/.bash_profile . As we can use an export command to define new bash variables we can
also use it to modify a bash prompt $PS1 variable. To change a current bash prompt to
display only time we could do:

export PS1="\t: "

Changing foreground and background bash prompt


colors
Syntax for changing colors in the bash is as follows:

3[ -> Indicates the beginning of color in the text


x;yzm - Indicates color code
3[00m - Indicates the end of color in the text

Bash color codes:

export PS1="3[01;31mBASH IN RED3[00m: "

5/6
Bash Prompt Examples
To get you started with your new bash prompt here are couple examples:

Display current Time


export PS1="\u@\h \t:$ "

Counting Files in the Current Directory


This bash prompt displays current number of files and directories in the current
directory.

export PS1="\u@\h [$(ls | wc -l)]:$ "

6/6

You might also like