Difference between revisions of "Changing bash prompt"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
Line 14: Line 14:
 
==Customizing Prompt==
 
==Customizing Prompt==
 
Bash shell allows prompt strings to be customized by inserting a number of backslash-escaped special characters. Quoting from the bash man page:
 
Bash shell allows prompt strings to be customized by inserting a number of backslash-escaped special characters. Quoting from the bash man page:
<pre>
+
{| style="cellpadding="20" cellspacing="0" border="1"  width="100%"
              \a    an ASCII bell character (07)
+
! style="background:#ffdead;"| Sequence
              \d    the date in "Weekday Month Date" format (e.g., "Tue May 26")
+
! style="background:#ffdead;"| Description
              \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
+
|\a     
                    time representation.  The braces are required
+
|An ASCII bell character (07)
              \e     an ASCII escape character (033)
+
|-
              \h    the hostname up to the first .
+
|\d     
              \H    the hostname
+
|The date in "Weekday Month Date" format (e.g., "Tue May 26")  
              \j    the number of jobs currently managed by the shell
+
|-
              \l     the basename of the shell’s terminal device name
+
|\e
              \n    newline
+
|An ASCII escape character (033)
              \r    carriage return
+
|-
              \s    the name of the shell, the basename of $0 (the portion following the final slash)
+
|\h     
              \t    the current time in 24-hour HH:MM:SS format
+
|The hostname up to the first .
              \T    the current time in 12-hour HH:MM:SS format
+
|-
              \@    the current time in 12-hour am/pm format
+
|\H     
              \A    the current time in 24-hour HH:MM format
+
|The hostname (FQDN)
              \u    the username of the current user
+
|-
              \v    the version of bash (e.g., 2.00)
+
|\j     
              \V    the release of bash, version + patch level (e.g., 2.00.0)
+
|The number of jobs currently managed by the shell
              \w     the current working directory, with $HOME abbreviated with a tilde
+
|-
              \W    the basename of the current working directory, with $HOME abbreviated with a tilde
+
|\l
              \!    the history number of this command
+
|The basename of the shell’s terminal device name
              \#    the command number of this command
+
|-
              \$    if the effective UID is 0, a #, otherwise a $
+
|\n     
              \nnn  the character corresponding to the octal number nnn
+
|Newline
              \\    a backslash
+
|-
              \[    begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
+
|\r     
              \]    end a sequence of non-printing characters</pre>
+
|Carriage return
 +
|-
 +
|\s     
 +
|The name of the shell, the basename of $0 (the portion following the final slash)
 +
|-
 +
|\t     
 +
|The current time in 24-hour HH:MM:SS format
 +
|-
 +
|\T     
 +
|The current time in 12-hour HH:MM:SS format
 +
|-
 +
|\@     
 +
|The current time in 12-hour am/pm format
 +
|-
 +
|\A     
 +
|The current time in 24-hour HH:MM format
 +
|-
 +
|\u     
 +
|The username of the current user
 +
|-
 +
|\v     
 +
|The version of bash (e.g., 2.00)
 +
|-
 +
|\V    T
 +
|The release of bash, version + patch level (e.g., 2.00.0)
 +
|-
 +
|\w
 +
|The current working directory, with $HOME abbreviated with a tilde
 +
|-
 +
|\W     
 +
|The basename of the current working directory, with $HOME abbreviated with a tilde
 +
|-
 +
|\!     
 +
|The history number of this command
 +
|-
 +
|\#     
 +
|The command number of this command
 +
|-
 +
|\$     
 +
|If the effective UID is 0, a #, otherwise a $
 +
|-
 +
|\nnn   
 +
|The character corresponding to the 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
 +
|-
 +
|\]     
 +
|End a sequence of non-printing characters</pre>
 +
|}
  
 
[[Category:Variables and Quoting]][[Category:Commands]]
 
[[Category:Variables and Quoting]][[Category:Commands]]

Revision as of 11:04, 20 October 2009

← Using aliasesHomeSetting shell options →

  • Task: You need to customize your bash prompt by editing PS1 variable.
  • Display, your current prompt setting, enter:
echo $PS1

Sample outputs:

\u@\h:\w\$
  • For testing purpose set PS1 as follows and notice the change:
PS1='your wish is my command : '

Sample outputs:

vivek@vivek-desktop:~$ PS1='your wish is my command : '
your wish is my command : 

Customizing Prompt

Bash shell allows prompt strings to be customized by inserting a number of backslash-escaped special characters. Quoting from the bash man page:

Sequence Description
\a An ASCII bell character (07)
\d The date in "Weekday Month Date" format (e.g., "Tue May 26")
\e An ASCII escape character (033)
\h The hostname up to the first .
\H The hostname (FQDN)
\j The number of jobs currently managed by the shell
\l The basename of the shell’s terminal device name
\n Newline
\r Carriage return
\s The name of the shell, the basename of $0 (the portion following the final slash)
\t The current time in 24-hour HH:MM:SS format
\T The current time in 12-hour HH:MM:SS format
\@ The current time in 12-hour am/pm format
\A The current time in 24-hour HH:MM format
\u The username of the current user
\v The version of bash (e.g., 2.00)
\V T The release of bash, version + patch level (e.g., 2.00.0)
\w The current working directory, with $HOME abbreviated with a tilde
\W The basename of the current working directory, with $HOME abbreviated with a tilde
\! The history number of this command
\# The command number of this command
\$ If the effective UID is 0, a #, otherwise a $
\nnn The character corresponding to the 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
\] End a sequence of non-printing characters

← Using aliasesHomeSetting shell options →