Command Prompt Overview

The command prompt looks like the following:

img

Command prompt

View Settings of Current Command Prompt

There are several ways we can view the settings for the command prompt.

$PS1 Variable

We can get the command-line prompt setting by printing the environment variable $PS1:

1
echo $PS1
img

Command prompt setting

set Command

1
set | grep PS1
img

Command prompt setting

Command Prompt Settings Modification

Temporarily Back Up the Original Settings

Let’s temporarily back up the current command prompt settings:

1
PS1_BAK=$PS1
img

Temporarily back up current command prompt's settings

img

Check the temporarily backed up command prompt's settings

Set Command Prompt to Show Nothing

1
PS1=   # Without specifying anything tells command prompt to show nothing
img

Empty the command prompt settings

Let’s check if the command-line prompt can still working properly:

1
echo 'The command prompt is still working'
img

The command prompt is still working

Set a Beep Sound

Every time the line breaks, the computer will make a sound:

1
PS1='\a'
img

Set a beep sound. We can use PS1= to reset the settings

Show Date in Week, Month, Date

We can use the special character \d to display weeks, months, and date at the command line prompt:

1
PS1='\d'
img

Show Week, Month, Date. We can use PS1= to reset the settings

Show Date in Customized Format

We can use the special character \D{format} to display customized date format at the command line prompt:

1
PS1='\D{%d/%m/%y}'
img

Show customized date format. We can use PS1= to reset the settings

Show Hostname Content Before the First Dot

We can use the special character \h to display the content before the first dot of the hostname at the command line prompt:

1
PS1='\h'
img

Show first part of the host. We can use PS1= to reset the settings

Show the Whole Hostname

We can use the special character \H to display the whole hostname at the command line prompt:

1
PS1='\H'
img

Show the whole hostname. We can use PS1= to reset the settings

Show the Number of Jobs Managed By Shell

We can use the special character \j to display the number of jobs managed by shell at the command line prompt:

1
PS1='\j'
img

Show the number of shell managed jobs. We can use PS1= to reset the settings

Show an Extra Line When Line Breaks

We can use the special character \n to display the an extra line at the command line prompt:

1
PS1='\n'
img

Show an extra line. We can use PS1= to reset the settings

Show the Name of the Shell

We can use the special character \s to display the name of the shell at the command line prompt:

1
PS1='\s'
img

Show the shell's name. We can use PS1= to reset the settings

Show the Time in Twenty-Four Hour Format

We can use the special character \t to display the time in 24-hour format at the command line prompt:

1
PS1='\t'
img

Show the 24-hour format time. We can use PS1= to reset the settings

We can use \A to show time without the second part:

1
PS1='\A'
img

Show the 24-hour format time without second part. We can use PS1= to reset the settings

Show the Time in Twelve Hour Format

We can use the special character \T to display the time in 12-hour format at the command line prompt:

1
PS1='\T'
img

Show the 12-hour format time. We can use PS1= to reset the settings

Show the Time in Twelve Hour AM/PM Format

We can use the special character \@ to display the time in 12-hour am/pm format at the command line prompt:

1
PS1='\@'
img

Show the 12-hour am/pm format time. We can use PS1= to reset the settings

Show the Name of the Current User

We can use the special character \u to display the name of the current user at the command line prompt:

1
PS1='\u'
img

Show the current user's name. We can use PS1= to reset the settings

Show the Version of the Bash

We can use the special character \v to display the version of the bash at the command line prompt:

1
PS1='\v'
img

Show the bash's version. We can use PS1= to reset the settings

Show the Release of the Bash

We can use the special character \V to display the release (version and patch level) of the bash at the command line prompt:

1
PS1='\V'
img

Show the bash's version and patch level. We can use PS1= to reset the settings

Show the Whole Current Working Directory

We can use the special character \w to display the whole path of current working directory at the command line prompt:

1
2
3
mkdir a    # let's create a directory to demonstrate
cd a    # change to that directory
PS1='\w'
img

Show the whole working directory path. We can use PS1= to reset the settings

Show Only the Current Working Directory

We can use the special character \W to display the whole path of current working directory at the command line prompt:

1
PS1='\W'
img

Show only the current working directory. We can use PS1= to reset the settings

Show the History Number of the Command

We can use the special character \! to display the history number of the command at the command line prompt:

1
PS1='\!'
img

Show the command's history number. We can use PS1= to reset the settings

Show the Command Number of the Command

We can use the special character \# to display the command number of the command at the command line prompt:

1
PS1='\#'
img

Show the command's command number. We can use PS1= to reset the settings

Show the Identity of a User

We can use the special character \$ to display the identity of a user at the command line prompt:

1
PS1='\$'

If the user’s uid equals to 0 then show #, otherwise show $:

img

Show the user's identity. We can use PS1= to reset the settings

Show a Backslash

We can use the special character \\ to display a backslash at the command line prompt:

1
PS1='\\'
img

Show a backslash. We can use PS1= to reset the settings

Restore from Backup

We can restore the setting from the PS1_BAK variable we created early:

1
PS1=$PS1_BAK
img

Restore settings from the backup.

Mimic the Default Settings

We can mimic the default setting:

1
PS1='[\u@\h \W]\$'
img

Mimic the default settings.

References 6.9 Controlling the Prompt

Buy me a coffeeBuy me a coffee