티스토리 뷰

Tip

Terminal Prompt 설정하기

do9dark 2015.04.02 14:07

프롬프트(Prompt)는 컴퓨터가 입력을 기다리고 있음을 가리키기 위해 화면에 나타나는 표시


명령어 중심의 시스템에서는 명령어를 받아들이기 위한 준비가 되었을 때, 미리 정해진 부호를 나타낸다. 

유닉스(리눅스)에서는 $ 또는 #를 나타낸다.


터미널을 사용할 때 프롬프트(Prompt)는 정보를 가지고 있으며, 

Bash(Bourne-again) Shell에서 프롬프트(Prompt) 정보는 PS1이라는 변수에 들어있다.


현재 사용하는 Shell은 환경변수를 통해서 확인이 가능하다.


$ echo $SHELL


같은 방법으로 PS1의 정보도 다음과 같이 확인할 수 있다.


$ echo $PS1


 현재 시스템에서 사용이 가능한 Shell의 종류는 다음과 같다.


$ cat /etc/shells


Shell 변경은 chsh 명령어를 통해서 할 수 있다.


$ chsh



프롬프트(Prompt)의 정보를 가지고 있는 PS1의 내용을 보면 다음과 같다.


\h:\W \u\$

{hostname}:{Working Directory} {user name}$ 


시간을 나타내고 싶다면 \t 를 사용하면 된다.


\t \u$

{시간} {로그인된 사용자}$


일시적으로 적용된 결과를 확인하고 싶을 때에는 다음과 같이 적용한다.


$ export PS1="(\t) \u$ "



다른 옵션 정보


 ▪ \a an ASCII bell character (07)

 ▪ \d the date in “Weekday Month Date” format (e.g., “Tue May 26″)

 ▪ \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

 ▪ \e an ASCII escape character (033)

 ▪ \h the hostname up to the first part

 ▪ \H the hostname

 ▪ \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 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 character

▪ $kernel_version The output of the uname -r command from $kernel_version variable

 ▪ \$? Status of the last command


 ▪ \e[ Indicates the beginning of color prompt

 ▪ x;ym    Indicates color code. Use the color code values mentioned below.

 ▪ \e[m    indicates the end of color prompt


tput Color Capabilities:

 ▪ tput setab [1-7] – Set a background color using ANSI escape

 ▪ tput setb [1-7] – Set a background color

 ▪ tput setaf [1-7] – Set a foreground color using ANSI escape

 ▪ tput setf [1-7] – Set a foreground color


tput Text Mode Capabilities:

 ▪ tput bold – Set bold mode

 ▪ tput dim – turn on half-bright mode

 ▪ tput smul – begin underline mode

 ▪ tput rmul – exit underline mode

 ▪ tput rev – Turn on reverse mode

 ▪ tput smso – Enter standout mode (bold on rxvt)

 ▪ tput rmso – Exit standout mode

 ▪ tput sgr0 – Turn off all attributes


Color Code for tput:

 ▪ 0 – Black

 ▪ 1 – Red

 ▪ 2 – Green

 ▪ 3 – Yellow

 ▪ 4 – Blue

 ▪ 5 – Magenta

 ▪ 6 – Cyan

 ▪ 7 – White



Shell Script를 프롬프트(Prompt)에서 사용할 수가 있다.


$ cat ~/bin/shell_scripting.sh

for filesize in $(ls -l . | grep "^-" | awk '{print $5}')
do
    let totalsize=$totalsize+$filesize
done
echo -"$totalsize"
cs

 

$ export PATH=$PATH:~/bin

$ export PS1="\$(shell_scripting.sh)"



설정을 저장하기 위해서는 ~/.profile 또는 ~/.bashrc 또는 ~/.bash_profile 에 PS1 정보를 저장하면 된다.

전체 사용자 설정은 /etc/bashrc 에 PS1 정보를 저장하면 된다.


$ vi ~/.bash_profile

export PS1="\[$(tput bold)\]\[$(tput setaf 1)\][\u:\W]\\$ \[$(tput sgr0)\]"
cs


export PS1="\[$(tput bold)$(tput setaf 1)\][\u:\W]\\$ \[$(tput sgr0)\]"
cs



저장한 정보를 적용하기 위해서는 아래와 같이 하면 된다.

$ . .bash_profile

또는 

$ source .bash_profile



더 자세한 정보는 WIKIPEDIA - ANSI escape code 에서 찾을 수 있다.

(https://en.wikipedia.org/wiki/ANSI_escape_code


ANSI escape code

In computingANSI escape code (or escape sequences) is a method using in-band signaling to control the formatting, color, and other output options on video text terminals. To encode this formatting information, it embeds certain sequences of bytes into the text, which the terminal looks for and interprets as commands, not as character codes.

ANSI codes were introduced in the 1970s and became widespread in the minicomputer/mainframe market by the early 1980s. They were used by the nascent bulletin board system market to offer improved displays compared to earlier systems lacking cursor movement, leading to even more widespread use.

Although hardware text terminals have become increasingly rare in the 21st century, the relevance of the ANSI standard persists because most terminal emulators interpret at least some of the ANSI escape sequences in the output text. One notable exception is the win32 console component of Microsoft Windows.

Sequence elements

Escape sequences start with the character ESC (ASCII decimal 27/hex 0x1B/octal 033). For two character sequences, the second character is in the range ASCII 6495 (@ to _).

However, most of the sequences are more than two characters, and start with the characters ESC and [ (left bracket). This sequence is called CSI for Control Sequence Introducer (or Control Sequence Initiator). The final character of these sequences is in the range ASCII 64126 (@ to ~).

There is a single-character CSI (155/0x9B /0233) as well. The ESC[ two-character sequence is more often used than the single-character alternative (for details see C0 and C1 control codes).

Only the two-character sequence is recognized by devices that support just ASCII (7-bit bytes) or devices that support 8-bit bytes, but use the 0x800x9F control character range for other purposes. On terminals that use UTF-8 encoding, both forms take 2 bytes (CSI in UTF-8 is 0xC20x9B)[discuss] but the ESC[ sequence is clearer.

Though some encodings use multiple bytes per character, the following discussion is restricted to ASCII characters, and thus assumes a single byte for each character.

Non-CSI codes

Note: other C0 codes besides ESC — commonly BEL, BS, CR, LF, FF, TAB, VT, SO, and SI — may produce similar or identical effects to some control sequences when output.

ESC N = SS2
ESC O = SS3
Select a single character from one of the alternate character sets.
ESC ^ = PM
ESC _ = APC
These each take a single string of text, terminated by ST (ESC \ ). They are ignored by xterm.
ESC P = DCS
Device control string,
ESC ] = OSC
Operating system command — these are similar to CSI, but not limited to integer arguments. Because they are frequently used,[citation needed] in many cases BEL is an acceptable alternative to ST. E.g., in xterm, the window title can be set by: "OSC0;this is the window titleBEL"

Note: pressing special keys on the keyboard, as well as outputting many xterm CSI, DCS, or OSC sequences, often produces a CSI, DCS, or OSC sequence.

CSI codes

The general structure of most ANSI escape sequences is CSI [private mode character(s?)n1 ; n2... [trailing intermediate character(s?)letter. The final byte, modified by private mode characters and trailing intermediate characters, specifies the command. The numbers are optional parameters. The default value used for omitted parameters varies with the command, but is usually 1 or 0. If trailing parameters are omitted, the trailing semicolons may also be omitted.

The final byte is technically any character in the range 64126 (hex 0x400x7E, ASCII @ to ~), and may be modified with leading intermediate bytes in the range 32 to 47 (hex0x20 to 0x2F).

The colon (0x3A) is the only character not a part of the general sequence. It was left for future standardization, so any sequence containing it should be ignored.

Although multiple private mode characters or trailing intermediates are permitted, there are no such known usages.[citation needed]

If there are any leading private mode characters, the main body of the sequence could theoretically contain any order of characters 0x300x3F instead of a well-formed semicolon-separated list of numbers,[citation needed] but all known terminals are nice and just use them as a flag.[citation needed] Sequences are also private if the final byte is in the range 112126 (hex0x700x7E, ASCII p~).

Examples of private escape codes include the DECTCEM (DEC text cursor enable mode) shown below. It was first introduced for the VT-300 series of video terminals.

The existence of a C0 control, DEL (0x7F), or a high characters is undefined.[clarification needed] Typically, implementations will either cancel the sequence or execute the control, and then continue parsing the CSI sequence.[clarification needed]

Some ANSI escape sequences (not a complete list)
CodeNameEffect

CSI n ACUU – Cursor UpMoves the cursor n (default 1) cells in the given direction. If the cursor is already at the edge of the screen, this has no effect.

CSI n BCUD – Cursor Down

CSI n CCUF – Cursor Forward

CSI n DCUB – Cursor Back
CSI n ECNL – Cursor Next LineMoves cursor to beginning of the line n (default 1) lines down. (not ANSI.SYS)
CSI n FCPL – Cursor Previous LineMoves cursor to beginning of the line n (default 1) lines up. (not ANSI.SYS)
CSI n GCHA – Cursor Horizontal AbsoluteMoves the cursor to column n. (not ANSI.SYS)

CSI n ; m HCUP – Cursor PositionMoves the cursor to row n, column m. The values are 1-based, and default to 1 (top left corner) if omitted. A sequence such as CSI ;5H is a synonym for CSI 1;5H as well as CSI 17;H is the same as CSI 17H and CSI 17;1H

CSI n JED – Erase DisplayClears part of the screen. If n is 0 (or missing), clear from cursor to end of screen. If n is 1, clear from cursor to beginning of the screen. If n is2, clear entire screen (and moves cursor to upper left on DOS ANSI.SYS).

CSI n KEL – Erase in LineErases part of the line. If n is zero (or missing), clear from cursor to the end of the line. If n is one, clear from cursor to beginning of the line. Ifn is two, clear entire line. Cursor position does not change.

CSI n SSU – Scroll UpScroll whole page up by n (default 1) lines. New lines are added at the bottom. (not ANSI.SYS)

CSI n TSD – Scroll DownScroll whole page down by n (default 1) lines. New lines are added at the top. (not ANSI.SYS)

CSI n ; m fHVP – Horizontal and Vertical PositionMoves the cursor to row n, column m. Both default to 1 if omitted. Same as CUP

CSI n mSGR – Select Graphic RenditionSets SGR parameters, including text color. After CSI can be zero or more parameters separated with ;. With no parameters, CSI m is treated asCSI 0 m (reset / normal), which is typical of most of the ANSI escape sequences.

CSI 5iAUX Port OnEnable aux serial port usually for local serial printer

CSI 4iAUX Port OffDisable aux serial port usually for local serial printer

CSI 6 nDSR – Device Status ReportReports the cursor position (CPR) to the application as (as though typed at the keyboard) ESC[n;mR, where n is the row and m is the column.)

CSI sSCP – Save Cursor PositionSaves the cursor position.

CSI uRCP – Restore Cursor PositionRestores the cursor position.
CSI ?25lDECTCEMHides the cursor. (Note: the trailing character is lowercase L.)
CSI ?25hDECTCEMShows the cursor.
SGR (Select Graphic Rendition) parameters
CodeEffectNote
0Reset / Normalall attributes off
1Bold or increased intensity
2Faint (decreased intensity)Not widely supported.
3Italic: onNot widely supported. Sometimes treated as inverse.
4Underline: Single
5Blink: Slowless than 150 per minute
6Blink: RapidMS-DOS ANSI.SYS; 150+ per minute; not widely supported
7Image: Negativeinverse or reverse; swap foreground and background (reverse video)
8ConcealNot widely supported.
9Crossed-outCharacters legible, but marked for deletion. Not widely supported.
10Primary(default) font
11–19n-th alternate fontSelect the n-th alternate font (14 being the fourth alternate font, up to 19 being the 9th alternate font).
20Frakturhardly ever supported
21Bold: off or Underline: DoubleBold off not widely supported; double underline hardly ever supported.
22Normal color or intensityNeither bold nor faint
23Not italic, not Fraktur
24Underline: NoneNot singly or doubly underlined
25Blink: off
26Reserved
27Image: Positive
28Revealconceal off
29Not crossed out
30–37Set text color (foreground)30 + x, where x is from the color table below
38Reserved for extended set foreground colortypical supported next arguments are 5;x where x is color index (0..255) or 2;r;g;b where r,g,b are red, green and blue color channels (out of 255)
39Default text color (foreground)implementation defined (according to standard)
40–47Set background color40 + x, where x is from the color table below
48Reserved for extended set background colortypical supported next arguments are 5;x where x is color index (0..255) or 2;r;g;b where r,g,b are red, green and blue color channels (out of 255)
49Default background colorimplementation defined (according to standard)
50Reserved
51Framed
52Encircled
53Overlined
54Not framed or encircled
55Not overlined
56–59Reserved
60ideogram underline or right side linehardly ever supported
61ideogram double underline or double line on the right sidehardly ever supported
62ideogram overline or left side linehardly ever supported
63ideogram double overline or double line on the left sidehardly ever supported
64ideogram stress markinghardly ever supported
65ideogram attributes offhardly ever supported, reset the effects of all of 6064
90–97Set foreground text color, high intensityaixterm (not in standard)
100–107Set background color, high intensityaixterm (not in standard)

Colors

Text colors (and SGR parameters in general) are manipulated using CSI n1 [;n2 [; ...]] m sequences, where each n1n2, ... is an SGR parameter as shown above. Thus, for instance, you use codes 30+i to specify foreground color, 40+i to specify background color, where i is the number in the desired color's column header in the table below. The following examples can be used with the printf utility, where \x1b[ implements the CSI: To switch the foreground color to black, use \x1b[30m; to switch to red, use \x1b[31m; utilizing the "bold" parameter, gray would be \x1b[30;1m; to get bold red, use \x1b[31;1m. To reset colors to their defaults, use \x1b[39;49m (or reset all attributes with\x1b[0m).

Color table[11]
Intensity01234567
NormalBlackRedGreenYellow[12]BlueMagentaCyanWhite
BrightBlackRedGreenYellowBlueMagentaCyanWhite

There are two other color standards CSS/HTML standard colors and X Window colors which standardize both the color names and associated RGB color values, but the escape sequence standard only specifies the color names, not RGB values. The chart below shows default RGB assignments for some common terminal programs, together with the CSS and the X Window System colors for these color names.[citation needed]

Color nameStandard VGA colorsWindows XP CMDTerminal.appPuTTYmIRCxtermCSS/HTMLX
NormalBlack0, 0, 00, 0, 00, 0, 00, 0, 00, 0, 00, 0, 00, 0, 00, 0, 0
Red170, 0, 0128, 0, 0194, 54, 33187, 0, 0127, 0, 0205, 0, 0255, 0, 0255, 0, 0
Green0, 170, 00, 128, 037, 188, 360, 187, 00, 147, 00, 205, 00, 255, 00, 128, 0
Brown/yellow170, 85, 0128, 128, 0173, 173, 39187, 187, 0252, 127, 0205, 205, 0255, 255, 0255, 255, 0
Blue0, 0, 1700, 0, 12873, 46, 2250, 0, 1870, 0, 1270, 0, 2380, 0, 2550, 0, 255
Magenta170, 0, 170128, 0, 128211, 56, 211187, 0, 187156, 0, 156205, 0, 205255, 0, 255255, 0, 255
Cyan0, 170, 1700, 128, 12851, 187, 2000, 187, 1870, 147, 1470, 205, 2050, 255, 2550, 255, 255
Gray170, 170, 170192, 192, 192203, 204, 205187, 187, 187210, 210, 210229, 229, 229255, 255, 255255, 255, 255
Bright/lightDarkgray85, 85, 85128, 128, 128129, 131, 13185, 85, 85127, 127, 127127, 127, 127
Red255, 85, 85255, 0, 0252,57,31255, 85, 85255, 0, 0255, 0, 0
Green85, 255, 850, 255, 049, 231, 3485, 255, 850, 252, 00, 255, 0144, 238, 144144, 238, 144
Yellow255, 255, 85255, 255, 0234, 236, 35255, 255, 85255, 255, 0255, 255, 0255, 255, 224225, 255, 224
Blue85, 85, 2550, 0, 25588, 51, 25585, 85, 2550, 0, 25292, 92, 255173, 216, 230173, 216, 230
Magenta255, 85, 255255, 0, 255249, 53, 248255, 85, 255255, 0, 255255, 0, 255
Cyan85, 255, 2550, 255, 25520, 240, 24085, 255, 2550, 255, 2550, 255, 255224, 255, 255224, 255, 255
White255, 255, 255255, 255, 255233, 235, 235255, 255, 255255, 255, 255255, 255, 255

The VGA column denotes the typical colors that are used when booting PCs and leaving them in their classical 80×25 text mode. The colors are different in the EGA/VGA graphic modes.

In July 2004, the blue colors of xterm changed,[13] RGB (0,0,205) → (0,0,238) for normal and (0,0,255) → (92,92,255) for bright. As of 2010, old xterm versions still linger on many computers though.[citation needed]

Xterm[14]GNOME Terminal[citation needed] and KDE's Konsole[15] support ISO-8613-3 24-bit foreground and background color setting[better source needed] Quoting one of the text-files in its source-tree:[16][better source needed]

   ESC[ … 38;2;<r>;<g>;<b> … m Select RGB foreground color
   ESC[ … 48;2;<r>;<g>;<b> … m Select RGB background color

In 256-color mode (ESC[38;5;<fgcode>m and ESC[48;5;<bgcode>m), the color-codes are the following:[citation needed]

   0x00-0x07:  standard colors (as in ESC [ 30–37 m)
   0x08-0x0F:  high intensity colors (as in ESC [ 90–97 m)
   0x10-0xE7:  6 × 6 × 6 = 216 colors: 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
   0xE8-0xFF:  grayscale from black to white in 24 steps

Xterm allows also to set the default foreground and background colors using[14]

   ESC]10;<foreground>BEL
   ESC]11;<background>BEL

where <foreground> and <background> are X color specifications, and BEL is the ASCII BEL character (code 7). The closing bracket instead of an opening bracket reveals that it belongs to the operating system control commands.

Examples

CSI 2 J — This clears the screen and, on some devices, locates the cursor to the y,x position 1,1 (upper left corner).

CSI 32 m — This makes text green. On MS-DOS, normally the green would be dark, dull green, so you may wish to enable Bold with the sequence CSI 1 m which would make it bright green, or combined as CSI 32 ; 1 m. MS-DOS ANSI.SYS uses the Bold state to make the character Bright; also the Blink state can be set (via INT 10, AX 1003h, BL 00h) to render the Background in the Bright mode. MS-DOS ANSI.SYS does not support SGR codes 90–97 and 100–107 directly.

CSI 0 ; 6 8 ; "DIR" ; 13 p — This reassigns the key F10 to send to the keyboard buffer the string "DIR" and ENTER, which in the DOS command line would display the contents of the current directory. (MS-DOS ANSI.SYS only) This was sometimes used for ANSI bombs. This is a private-use code (as indicated by the letter p), using a non-standard extension to include a string-valued parameter. Following the letter of the standard would consider the sequence to end at the letter D.

CSI s — This saves the cursor position. Using the sequence CSI u will restore it to the position. Say the current cursor position is 7(y) and 10(x). The sequence CSI s will save those two numbers. Now you can move to a different cursor position, such as 20(y) and 3(x), using the sequence CSI 20 ; 3 H or CSI 20 ; 3 f. Now if you use the sequence CSI u the cursor position will return to 7(y) and 10(x). Some terminals require the DEC sequences ESC 7 / ESC 8 instead which is more widely supported.

Example of use in shell scripting

ANSI escape codes are often used in UNIX and UNIX-like terminals to provide syntax highlighting. For example, on compatible terminals, the following list command color-codes file and directory names by type.

ls --color

Users can employ escape codes in their scripts by including them as part of standard output or standard error. For example, the following sed command embellishes the output of the makecommand by displaying lines containing words starting with "WARN" in reverse video and words starting with "ERR" in bright yellow on a dark red background (letter case is ignored). The representations of the codes are highlighted.[17]

make 2>&1 | sed -e 's/.*\bWARN.*/\x1b[7m&\x1b[0m/i' -e 's/.*\bERR.*/\x1b[93;41m&\x1b[0m/i'

The following shell function flashes the terminal (by alternately sending reverse and normal video mode codes) until the user presses a key.[18]

flasher () { while true; do printf \\e[?5h; sleep 0.1; printf \\e[?5l; read -s -n1 -t1 && break; done; }

This can be used to alert a programmer when a lengthy command terminates, such as with make ; flasher .[19]

Invalid and ambiguous sequences in use

  • The Linux console uses OSC P n rr gg bb to change the palette, which, if hard-coded into an application, may hang other terminals. However, appending ST will be ignored by Linux and form a proper, ignorable sequence for other terminals.
  • On the Linux console, certain function keys generate sequences of the form CSI [ char. The CSI sequence should terminate on the [.
  • Old versions of Terminator generate SS3 1; modifiers char when F1–F4 are pressed with modifiers. The faulty behavior was copied from GNOME Terminal.[citation needed]
  • xterm replies CSI row ; column R if asked for cursor position and CSI 1 ; modifiers R if the F3 key is pressed with modifiers, which collide in the case of row == 1. This can be avoided by using the ? private modifier, which will be reflected in the response.[clarification needed]
  • many terminals prepend ESC to any character that is typed with the alt key down. This creates ambiguity for uppercase letters and symbols @[\]^_, which would form C1 codes.[clarification needed]
  • Konsole generates SS3 modifiers char when F1–F4 are pressed with modifiers.[clarification needed]



댓글
댓글쓰기 폼
«   2019/10   »
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
링크
공지사항
Total
85,063
Today
81
Yesterday
126