본문 바로가기
e-Logbook

ANSI Escape code를 이용하여 프롬프트를 이쁘게 바꾸기(PS1 셋팅하기)

by Thdnice 2012. 8. 10.
반응형


ANSI

  ANSI는 American National Standards Institute로 미국 표준 협회라는 뜻이다. 여기에서 보통 안시코드로 잘 알려져있는 ANSI Escape Code를 개발하여 화면에 글자색이나 바탕색을 제어할 수 있게 만들었다. 보통 안시코드로 화면의 색을 지정할 때에는 


^[[숫자m 


 과 같은 형식을 사용한다. 여기에서 ^[[는 키보드로 ^, [, [ 를 타이핑 하는게 아니라 Ctrl+v , Ctrl+[ 키를 치면 나오는 일종의 문자에 해당된다. 인터넷에서 검색하면 무수히 많은 색상표를 열람할 수 있지만 실제적으로 사용되는 색은 검은색(30), 붉은색(31), 초록색(32), 하늘색(36) 정도이다. 그리고 제일 중요한것은 기본화면색 (0)이다.




그럼 터미널에서의 프롬프트는?
  

  터미널에서 사용되는 프롬프트는 보통 환경변수 파일에 들어있는 PS1 이라는 시스템 변수를 수정함으로서 고칠 수 있다. 일반적으로 프롬프트 변수에는 호스트네임, 사용자네임, 그리고 현재 위치정도가 표현되는데, 이를 확인하기 위해서 echo명령을 통해 현재 적용중인 PS1변수 설정을 확인해보면.


echo $PS1   

\[\e[32;1m\]\u \[\e[36;1m\]\W \$ \[\e[0m\]


 다음과 같은 값을 얻을 수 있다. 일단 프롬프트에 들어갈 수 있는 것들과 그 내용은 다음과 같다.

\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 '.'
\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 characters


보면 알 수 있지만.. ANSI Escape Character을 /e로 대체할 수 있다. 즉 ^[[ 과 /e가 같은 뜻이라고 보면 된다.

따라서 내가 사용중인 환경변수를 해석해보면 다음과 같다. 

\[\e[32;1m\]\u \[\e[36;1m\]\W \$ \[\e[0m\]


 일단 처음의 \[ 부터 \] 까지는 color scheme의 시작과 끝을 나타낸다. 

32;1 의 뜻은 32(초록색)에 ;1 bold intensity를 적용한다는 뜻이다. 초록색에 bold intensity가 적용되면 밝은 초록색이 되게 된다.  보통 XX;YY 일때 XX는 글자색 , YY는 배경색을 뜻한다. 그런데, 여기에 1은 bold intensity를 뜻하고, 7은 역상을 뜻한다.

 \u 는 유저 네임이다. 즉 내가 사용자인지, root계정인지, 방문자 계정인지 등을 나타내준다.

혼자 사용하는 노트북에서는 사실 \h (호스트)가 별 의미 없으므로 사용하지 않았다. 그리고 \W는 경로를 의미하는데 \w를 사용할 경우에는 전체경로가 나타나지만 \W는 현재 작업 디랙토리만 출력하므로 나같은 경우는 \W를 더 선호한다. 

 마지막의 ^[[0m  은 다시 기본 화면색으로 돌려주는 것으로, 마지막에 이 내용을 생략하면 타이핑 하는 글자가 모두 색이 입혀져서 나오게 된다.  





ANSI 코드의 활용

  방금은 ANSI코드를 사용해서 프롬프트에 색을 입혔지만, 사실 ANSI 코드는 좀더 광범위한 부분에 사용가능하다. 특히 메세지를 출력해야하는 경우에 echo 등과 함께 같이 사용하면 많은 문장을 좀더 보기좋게 출력해 낼 수 있다.  다만 너무 많은 색의 사용은 오히려 눈만 현란하게 만들고 가독성을 저하시키므로 흑, 적, 녹, 황 정도의 4~5가지 색만 사용해서 이쁜 결과물을 만들도로 노력하는게 중요하겠다. 










반응형

댓글