터미널을 열면 나오는 명령어 입력줄을 자신의 입맛에 맞게 변경하는 방법입니다. 오래되서 기억이 흐릿하지만, 가장 초기엔 컴퓨터 이름과 사용자 이름을 보여주었던 것 같습니다.
Last login: Fri Jul 28 18:22:26 on ttys006 ~ (501) $ <- 여기
명령어 입력줄 변경하기
- 명령어 입력줄을 가리켜, ‘Command Prompt’ 혹은 ‘Shell Prompt’라고 이야기합니다.
명령어 입력줄을 처음 접하게 되면, 보통, ‘누구누구’s MacBook’ 혹은 ‘iMac’ 등으로 나타날 것입니다. 매번, 터미널 명령어 입력줄을 확인할 때에는 굳이 신경 쓸 필요없는 내용이죠. 아래 표기된 부분을 몇가지 방법을 활용하여 변경 해보도록 하겠습니다.
Last login: Fri Jan 11 22:51:34 on ttys000 Fri Jan 11 23:03:17 KST 2019 iMac:~ Travelholics$ <- 여기
PS1=”what?” | 임시 변경
‘PS1’는 쉘 변수이자 우리가 사용하는 명령어 입력줄의 모양을 결정합니다. 이 변수에 값을 대입하는 것으로, 명령어 입력줄의 임시로 변경하실 수 있습니다. 임시라는 말에서 눈치채셨겠지만, ‘=’ 대입 연산자를 통한 명령어 입력줄 변경은 터미널을 종료하는 순간 초기화됩니다. 붉은색은 명령어 입력줄의 변경된 모습을 나타냅니다.
- 명령어 입력줄을 ‘what?’으로 변경 ▼
PS1="what?" //what?은 필자가 정한 임의의 텍스트입니다.
Last login: Fri Jul 28 17:31:01 on ttys006
iMac:~ Travelholics$ PS1="what?"
what?
나만의 문구로 변경되긴 했지만, 현재 접속된 경로조차 알 수가 없습니다. 해당 경로를 나타내는 ‘/w’ (Escape Sequence)를 추가하여 PS1 변수를 변경해보도록 하겠습니다.
- 명령어 입력줄 : ‘\w what?’ ▼
what?PS1="\w what?"
~ what?cd Sites
~/Sites what?
현재 작업 중인 디렉토리를 나타내는 ‘\w’ 외에도 수많은 Escape Sequence가 있습니다. 일반 사용자들에게 많이 사용되는 아이템은 붉은색으로 표시해두었습니다. 참조 바랍니다. 제가 개인적으로 선호하는 옵션은 PS1=”\w (\!) \$” 입니다.
- Escape Sequence 예시 – 출처 ▼
\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 \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 \u the username of the current user \v the version of bash (e.g., 2.00) \V the release of bash, version + patchlevel (e.g., 2.00.0) \w the current working directory \W the basename of the current working directory \! 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
.bash_profile 손보기 | 영구 적용
위에서 언급한 PS1 쉘 변수를 ‘=’ 대입 연산자를 활용하여 아무리 멋지게 꾸며놓더라도, 터미널 종료와 함께 명령어 입력줄은 초기화됩니다. 따라서, 대입 연산자를 활용한 방법은 사용자 입장에서 터미널에 접근하였을 때, 임시로 빠르게 변경할 때 사용하기 적합한 방법입니다.
영구적인 사용을 위해선, .bash_profile 혹은 .bashrc 파일에 PS1 변수 값을 조정해주셔야 합니다. 필자는 Bash Shell만 사용해보았기 때문에, Bash Shell 기준으로 적어두었습니다. 편의상, macOS 환경에서 작성되었습니다.
- 모든 사용자에게 적용 /etc/bashrc ▼
sudo vim /etc/bashrc
# System-wide .bashrc file for interactive bash(1) shells. if [ -z "$PS1" ]; then return fi PS1='\h:\W \u\$ ' # Make bash check its window size after a process completes shopt -s checkwinsize [ -r "/etc/bashrc_$TERM_PROGRAM" ] && . "/etc/bashrc_$TERM_PROGRAM"
- 현재 사용자에게만 적용 ~/.bash_profile ▼
sudo vim ~/.bash_profile
export PS1="\w (\!) \$" //추가할 문구 " " 안의 내용은 스스로 결정
예시 ▼
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home export CLASSPATH=$CLASSPATH:/library/apache-tomcat/lib/servlet-api.jar export PS1="\w (\!) \$" export LESS='M' export PATH="$HOME/.composer/vendor/bin:$PATH" alias ls="/bin/ls -aF" alias m2u="tr '\015' '\012' " alias u2m="tr '\012' '\015' " alias mysql="/usr/local/mysql/bin/mysql"
기타 명령어
표제 관련해서 도움될만한 명령어들입니다. 모자란 부분이 있다면, 댓글로 알려주시면 감사 드립니다.
- 현재 설정된 명령어 입력줄 설정 확인 ▼
실행 : echo $PS1 결과 : \w (\!) $
- 현재 적용된 환경 변수 확인 ▼
실행 : env 결과 : (길어서 생략)
이상입니다.
[…] “iMac:~ SeongjuneKim$” 이 설정 값입니다. PS1과 관련된 자세한 내용은 링크를 참조 […]
[…] Prompt Command가 iMac:~ newuser$로 뜨네요. Prompt창을 Customization하는 방법은 링크를 […]