bourne shell prompt cis 218. bash prompt prompt is controlled via a special shell variable ps1, ps2,...

11
Bourne Shell Prompt CIS 218

Upload: sibyl-sutton

Post on 17-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Bourne Shell Prompt CIS 218. BASH Prompt Prompt is controlled via a special shell variable PS1, PS2, PS3 and PS4. If set, the value is executed as a command

Bourne Shell Prompt

CIS 218

Page 2: Bourne Shell Prompt CIS 218. BASH Prompt Prompt is controlled via a special shell variable PS1, PS2, PS3 and PS4. If set, the value is executed as a command

BASH Prompt• Prompt is controlled via a special shell variable PS1, PS2, PS3 and PS4. If set, the

value is executed as a command prior to issuing each primary prompt.

PS1 - This parameter is used as the primary prompt string. The default is: \s-\v\$ .

PS2 - The parameter is used as line continuation. The default is: >

PS3 - The value of this parameter is used as the prompt for the select command.

PS4 - This parameter is displayed during an execution trace (sh –x). The default is +

• Under bash, you can set your prompt by changing the value of the PS1 environment

variable, as follows: export PS1="> "

• Changes take effect immediately, and can be made permanent by placing the "export" definition in your ~/.bashrc file. PS1 can contain any amount of plain text that you'd like.

• Most custom prompts contain information like the current username, working directory, or hostname. For example, the following prompt will display your username and hostname: $ export PS1="\u@\H > “

Note: we insert the username and hostname into the prompt by using special backslash-escaped character sequences that bash replaces with specific values when they appear in the PS1 variable sequences: "\u" (for username) and "\H" (for the first part of the hostname).

Page 3: Bourne Shell Prompt CIS 218. BASH Prompt Prompt is controlled via a special shell variable PS1, PS2, PS3 and PS4. If set, the value is executed as a command

BASH Prompt Examples• You display the command prompt(s) as follows:

echo $PS1

• You modify the command prompt(s) by assigning a new value to PS1:PS1="Hello World : "

• Set the prompt so that it can display today’d date and hostname:

PS1="\d \h $ “

• Set the prompt to display date/time, hostname and current directory:

$ PS1="[\d \t \u@\h:\w ] $ "

Page 4: Bourne Shell Prompt CIS 218. BASH Prompt Prompt is controlled via a special shell variable PS1, PS2, PS3 and PS4. If set, the value is executed as a command

BASH Prompt control charactersSequence Description• \a The ASCII bell character (you can also type \007)• \d Date in "Wed Sep 06" format• \e ASCII escape character (you can also type \033)• \h First part of hostname (such as "mybox")• \H Full hostname (such as "mybox.mydomain.com")• \j The number of processes you've suspended in this shell by hitting ^Z• \l The name of the shell's terminal device (such as "ttyp4")• \n Newline• \r Carriage return• \s The name of the shell executable (such as "bash")• \t Time in 24-hour format (such as "23:01:01")• \T Time in 12-hour format (such as "11:01:01")• \@ Time in 12-hour format with am/pm• \u Your username• \v Version of bash (such as 2.04)• \V Bash version, including patchlevel• \w Current working directory (such as "/home/drobbins")• \W The "basename" of the current working directory (such as "drobbins") • \! Current command's position in the history buffer• \# Command number (this will count up at each prompt, as long as you type something)• \$ If you are not root, inserts a "$"; if you are root, you get a "#"• \xxx Inserts an ASCII character based on three-digit number xxx (replace unused digits with zeros, such

as "\007")• \\ A backslash• \[ This sequence should appear before a sequence of characters that don't move the cursor (like color

escape sequences). This allows bash to calculate word wrapping correctly.• \] This sequence should appear after a sequence of non-printing characters.

Page 5: Bourne Shell Prompt CIS 218. BASH Prompt Prompt is controlled via a special shell variable PS1, PS2, PS3 and PS4. If set, the value is executed as a command

BASH Prompt Colorization

• Adding color is simple; all you do is add special escape sequences that'll be recognized by the terminal (rather than bash) and cause it to display certain parts of the text in color. Standard Linux terminals and X terminals allow you to set the foreground (text) color and the background color, and also enable "bold" characters if so desired. We get eight colors to choose from.

• Colors are selected by adding special sequences to PS1 -- basically sandwiching numeric values between a "\e[" (escape open-bracket) and an "m". If more than one numeric code, separate each code with a semicolon.

Example color code: "\e[0m“

• When we specify a zero as a numeric code, it tells the terminal to reset foreground, background, and boldness settings to their default values. You'll want to use this code at the end of your prompt, so that the text that you type in is not colorized.

Page 6: Bourne Shell Prompt CIS 218. BASH Prompt Prompt is controlled via a special shell variable PS1, PS2, PS3 and PS4. If set, the value is executed as a command

Color chart• To add colors to the shell prompt use the following export command syntax:

export '\e[x;ym $PS1 \e[m'Where: \e[ Start color scheme x;y Color pair to use (x;y) $PS1 is your shell prompt \e[m Stop color scheme

• x is 0 for regular, 1 for highlight.

• y is the color you'd like to use, and find the corresponding foreground (30-37) and background (40-47) numbers.

• Foreground: 30 - Black, 31 - Red, 32 - Green, 33 - Yellow, 34 - Blue, 35 - Purple, 36 - Blue/Green, 37 – White

• Background: 40 - Black, 41 - Red, 42 - Green, 43 - Yellow, 44 - Blue, 45 - Purple, 46 - Blue/Green, 47 – White

Page 7: Bourne Shell Prompt CIS 218. BASH Prompt Prompt is controlled via a special shell variable PS1, PS2, PS3 and PS4. If set, the value is executed as a command

BASH Prompt Color Examples• To set a red color prompt, type the command:

export PS1="\e[0;31m[\u@\h \W]\$ \e[m “

• For example, if you like green on a normal black background, the numbers are 32 and 40. Then add the appropriate color codes take your prompt: export PS1="\e[32;40m\w> “

• After bash prints the working directory, set the color back to normal with a "\e[0m" sequence:

export PS1="\e[32;40m\w> \e[0m“

export PS1="\[\e[32;1m\]\w> \[\e[0m\]"

export PS1="\[\e[36;1m\]\u@\[\e[32;1m\]\H> \[\e[0m\]"

Page 8: Bourne Shell Prompt CIS 218. BASH Prompt Prompt is controlled via a special shell variable PS1, PS2, PS3 and PS4. If set, the value is executed as a command

tput command• The tput utility uses the terminfo database to make the values of terminal-dependent capabilities to initialize or

reset the terminal, or return the long name of the requested terminal type. tput outputs a string if the attribute (capability name) is of type string, or an integer if the attribute is of type integer.

• You can also use tput command display pecfic comands to a RED prompt follows (depends on termial type):export PS1="\[$(tput setaf 1)\]\u@\h:\w $ \[$(tput sgr0)\]“

• tput bold - Bold effect • tput rev - Display inverse colors • tput sgr0 - Reset everything • tput init Initialize the terminal according to the type of terminal in the environmental variable TERM. • tput -Tvt100 reset Reset an vt100 terminal.• tput cup 0 0 Send the sequence to move the cursor to row 0, column 0, known as the "home" cursor position).• tput clear Echo the clear-screen sequence for the current terminal.• tput cols Print the number of columns for the current terminal.• tput -Tvt100 cols Print the number of columns for the 450 terminal.• bold=`tput smso` offbold=`tput rmso`

Set the shell variables bold, to begin stand-out mode sequence, and offbold, to end standout mode sequence, for the current terminal. This might be followed by a prompt: echo "${bold}Please type in your name: ${offbold}\c"

• tput hc Set exit code to indicate if the current terminal is a hard copy terminal.• tput cup 23 4 Send the sequence to move the cursor to row 23, column 4.• tput cup Send the terminfo string for cursor-movement, with no parameters substituted.• tput longname Print the long name from the terminfo database for the type of terminal specified in the

environmental variable TERM.• tput setaf {CODE}- Set foreground color, see color {CODE} below • tput setab {CODE}- Set background color, see color {CODE} below • {code} 0=Black, 1=Red, 2=Green, 3=Yellow, 4=Blue, 5=Magenta, 6=Cyan, 7=White

Page 9: Bourne Shell Prompt CIS 218. BASH Prompt Prompt is controlled via a special shell variable PS1, PS2, PS3 and PS4. If set, the value is executed as a command

tset• tset initializes terminals. tset first determines the type of terminal that you are using. This determination is made in

the following order using the first terminal type found.

1. The terminal argument specified on the command line.

2. The value of the TERM environmental variable.

3. (BSD systems only.) The terminal type associated with the standard error output device in the /etc/ttys file.

4. The default terminal type, ``unknown''.

• tset options• -q The terminal type is displayed to the standard output, and the terminal is not initialized in any way. • -I Do not send the terminal or tab initialization strings to the terminal.• -Q Don't display any values for the erase, interrupt and line kill characters.• -V reports the version of ncurses which was used in this program, and exits.• -i Set the interrupt character to ch.• -k Set the line kill character to ch.• -m Specify a mapping from a port type to a terminal. See below for more information.• -r Print the terminal type to the standard error output.• -s Print the sequence of shell commands to initialize the environment variable TERM to the standard output.

• The arguments for the -e, -i, and -k options may either be entered as actual characters or by using the `hat' notation, i.e. control-h may be specified as ``^H'' or ``^h''.

• To enter the terminal type and information about the terminal's capabilities into the shell's environment, this is done using the -s option as follows .login (csh) or .profile (bash) files: eval `tset -s options ... `

•  • When invoked as reset command, tset sets cooked and echo modes, <LF>reset<LF>

Page 10: Bourne Shell Prompt CIS 218. BASH Prompt Prompt is controlled via a special shell variable PS1, PS2, PS3 and PS4. If set, the value is executed as a command

stty

• The stty command is used for changing the settings of a Unix computer terminal. This command is used to change keystrokes, irregular character handling, and more. stty gives a full set of features that are also available

stty list stty settings

stty erase ^H Let the key combination Ctrl+H (in caret notation ^H) erase

stty -a List all terminal attributes

stty -g Save all current terminal settings in stty readable format.

stty -echo Turn off screen echo (like password entry)

stty -F `tty` rows 35 columns 59 To change the row and column size of your pseudo terminal

stty -raw Set terminal to "uninitialized" mode

stty sane Restore terminal device to normal settings

Page 11: Bourne Shell Prompt CIS 218. BASH Prompt Prompt is controlled via a special shell variable PS1, PS2, PS3 and PS4. If set, the value is executed as a command

Making prompt settings permanent

• $ vi .bash_profileOR$ vi .bashrc

• Append export line:export PS1="\e[0;31m[\u@\h \W]\$ \e[m"

• Save and close the file.