ºìÁªLinuxÃÅ»§
Linux°ïÖú

Linux¼¼ÇÉ£ºshellÌáʾÐÐħÊõ

·¢²¼Ê±¼ä:2005-12-07 17:24:48À´Ô´:ºìÁª×÷Õß:linux_
Èç¹ûÄúºÜÈÝÒ×ʹ shell ÌáʾÐбäµÃÉ«²ÊѤÀÃìµÇÒ´øÓиü¶àÐÅÏ¢£¬ÎªÊ²Ã´»¹Òª¼á³ÖÓõ¥µ÷µÄ±ê×¼ shell ÌáʾÐÐÄØ£¿ÔÚÕâƪ¼¼ÇÉÖУ¬Daniel Robbins ½«ËµÃ÷ÈçºÎ»ñµÃ·ûºÏÄúµÄÒâÔ¸µÄ shell ÌáʾÐУ¬²¢»á˵Ã÷ÈçºÎ¶¯Ì¬¸üРX Öն˵ıêÌâÀ¸¡£

×÷Ϊ Linux/UNIX ÈË£¬ÎÒÃÇÓкܳ¤µÄʱ¼äÊÇÔÚ shell Öй¤×÷£¬²¢ÇÒÔÚÐí¶àÇé¿öÏ£¬ÏÂÃæÕâÒ»ÐоÍÊÇʼÖÕ¶¢×ÅÎÒÃǵÄÄǸöÌáʾÐУº

bash-2.04$

Èç¹ûÄúÇ¡ÇÉÊdz¬¼¶Óû§ (root)£¬Äú¾ÍÓÐȨʹÓÃÏÂÃæÕâ¸öÃÀÀöµÄ±êʾ¡°Éí·Ý¡±µÄÌáʾÐа汾£º

bash-2.04#

ÕâЩÌáʾÐв¢²»ÊÇÊ®·ÖƯÁÁ¡£ÕâÒ²¾ÍÄѹּ¸ÖÖ Linux °æ±¾¶ÔĬÈÏÌáʾÐнøÐÐÁËÉý¼¶£¬ÔÚÆäÖÐÔö¼ÓÁËÑÕÉ«ºÍ¸ü¶àµÄÐÅÏ¢¡£µ«ÊÇ£¬¼´±ãÄúÇ¡ºÃÓÐÒ»¸ö±¾Éí´øÓкܺõIJÊÉ«ÌáʾÐеÄÐÂʽ°æ±¾£¬ËüÒ²²»¿ÉÄÜÊÇÍêÃÀÎÞȱµÄ¡£Äú»òÐíÏ£ÍûÔÚÌáʾÐÐÖÐÔö¼Ó»ò¸ü¸Ä¼¸ÖÖÑÕÉ«£¬»òÕßÔö¼Ó£¨»òɾ³ý£©Ò»Ð©ÐÅÏ¢¡£´ÓÍ·¿ªÊ¼Éè¼ÆÊôÓÚÄú×Ô¼ºµÄ²ÊÉ«µÄ¡¢¾­¹ý×°ÊεÄÌáʾÐв¢²»ÄÑ¡£
ÌáʾÐлù´¡
ÔÚ bash Ï£¬¿ÉÒÔͨ¹ý¸ü¸Ä PS1 »·¾³±äÁ¿µÄÖµÀ´ÉèÖÃÌáʾÐУ¬ÈçÏÂËùʾ£º
$ export PS1="> "
>

¸ü¸Ä»áÁ¢¼´ÉúЧ£¬Í¨¹ý½« "export" ¶¨Òå·ÅÔÚÄúµÄ ~/.bashrc ÎļþÖпɽ«ÕâÖÖ¸ü¸Ä¹Ì¶¨ÏÂÀ´¡£Ö»ÒªÄúÔ¸Ò⣬PS1 ¿ÉÒÔ°üº¬ÈÎÒâÊýÁ¿µÄ´¿Îı¾£º
$ export PS1="This is my super prompt > "
This is my super prompt >

¾¡¹ÜÕâºÜÓÐȤ£¬µ«ÔÚÌáʾÐÐÖаüº¬´óÁ¿¾²Ì¬Îı¾²¢²»ÊÇÌرðÓÐÓᣴó¶àÊý¶¨ÖƵÄÌáʾÐаüº¬ÖîÈçÓû§Ãû¡¢¹¤×÷Ŀ¼»òÖ÷»úÃûÖ®ÀàµÄÐÅÏ¢¡£ÕâЩ»¨ÐõÐÅÏ¢¿ÉÒÔ°ïÖúÄúÔÚ shell ÊÀ½çÖÐåÛÓΡ£ÀýÈ磬ÏÂÃæµÄÌáʾÐн«ÏÔʾÄúµÄÓû§ÃûºÍÖ÷»úÃû£º

$ export PS1="u@H > "
drobbins@freebox >

Õâ¸öÌáʾÐжÔÓÚÄÇЩÒÔ¶à¸ö²»Í¬Ãû³ÆµÄÕÊ»§µÇ¼¶ą̀»úÆ÷µÄÈËÓÈΪÓÐÓã¬ÒòΪËü¿ÉÒÔÌáÐÑÄú£ºÄúÄ¿Ç°ÔÚÄĄ̈»úÆ÷ÉϲÙ×÷£¬ÓµÓÐʲôȨÏÞ¡£
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃÁËרÓõÄÓ÷´Ð±¸ÜתÒåµÄ×Ö·ûÐòÁУ¬½å´Ë֪ͨ bash ½«Óû§ÃûºÍÖ÷»úÃû²åÈëÌáʾÐÐÖУ¬µ±ÕâЩתÒå×Ö·ûÐòÁгöÏÖÔÚ PS1 ±äÁ¿ÖÐʱ£¬bash ¾Í»áÓÃÌض¨µÄÖµÌæ»»ËüÃÇ¡£ÎÒÃÇʹÓÃÁËÐòÁÐ "u"£¨±íʾÓû§Ãû£©ºÍ "H"£¨±íʾÖ÷»úÃûµÄµÚÒ»²¿·Ö£©¡£ÏÂÃæÊÇ bash ¿Éʶ±ðµÄÈ«²¿×¨ÓÃÐòÁеÄÍêÕûÁÐ±í£¨Äú¿ÉÒÔÔÚ bash man page µÄ "PROMPTING" ²¿·ÖÕÒµ½Õâ¸öÁÐ±í£©£º
ÐòÁР˵Ã÷
a ASCII ÏìÁå×Ö·û£¨Ò²¿ÉÒÔ¼üÈë 07£©
d "Wed Sep 06" ¸ñʽµÄÈÕÆÚ
e ASCII תÒå×Ö·û£¨Ò²¿ÉÒÔ¼üÈë 33£©
h Ö÷»úÃûµÄµÚÒ»²¿·Ö£¨Èç "mybox"£©
H Ö÷»úµÄÈ«³Æ£¨Èç "mybox.mydomain.com"£©
j ÔÚ´Ë shell ÖÐͨ¹ý°´ ^Z ¹ÒÆðµÄ½ø³ÌÊý
l ´Ë shell µÄÖÕ¶ËÉ豸Ãû£¨Èç "ttyp4"£©
n »»Ðзû
r »Ø³µ·û
s shell µÄÃû³Æ£¨Èç "bash"£©
t 24 СʱÖÆʱ¼ä£¨Èç "23:01:01"£©
T 12 СʱÖÆʱ¼ä£¨Èç "11:01:01"£©
@ ´øÓÐ am/pm µÄ 12 СʱÖÆʱ¼ä
u ̞
v bash µÄ°æ±¾£¨Èç 2.04£©
V Bash °æ±¾£¨°üÀ¨²¹¶¡¼¶±ð£© ?/td>
w µ±Ç°¹¤×÷Ŀ¼£¨Èç "/home/drobbins"£©
W µ±Ç°¹¤×÷Ŀ¼µÄ¡°»ùÃû (basename)¡±£¨Èç "drobbins"£©
! µ±Ç°ÃüÁîÔÚÀúÊ·»º³åÇøÖеÄλÖÃ
# ÃüÁî±àºÅ£¨Ö»ÒªÄú¼üÈëÄÚÈÝ£¬Ëü¾Í»áÔÚÿ´ÎÌáʾʱÀÛ¼Ó£©
$ Èç¹ûÄú²»Êdz¬¼¶Óû§ (root)£¬Ôò²åÈëÒ»¸ö "$"£»Èç¹ûÄúÊdz¬¼¶Óû§£¬ÔòÏÔʾһ¸ö "#"
xxx ²åÈëÒ»¸öÓÃÈýλÊý xxx£¨ÓÃÁã´úÌæδʹÓõÄÊý×Ö£¬Èç "07"£©±íʾµÄ ASCII ×Ö·û
·´Ð±¸Ü
[ Õâ¸öÐòÁÐÓ¦¸Ã³öÏÖÔÚ²»Òƶ¯¹â±êµÄ×Ö·ûÐòÁУ¨ÈçÑÕɫתÒåÐòÁУ©Ö®Ç°¡£Ëüʹ bash Äܹ»ÕýÈ·¼ÆËã×Ô¶¯»»ÐС£
] Õâ¸öÐòÁÐÓ¦¸Ã³öÏÖÔÚ·Ç´òÓ¡×Ö·ûÐòÁÐÖ®ºó¡£
ÕâÑù£¬ÄúÒѾ­ÖªµÀÁË bash ÖÐÓ÷´Ð±¸ÜתÒåµÄÈ«²¿×¨ÓÃÐòÁС£ÇëÉÔ΢ÑÝÁ·Ò»ÏÂÕâЩÐòÁУ¬ÒÔ¶ÔËüÃǵŤ×÷·½Ê½»ñµÃһЩ¸ÐÐÔÈÏʶ¡£ÔÚÄú×öÁËһЩ²âÊÔÖ®ºó£¬ÏÂÃ濪ʼÌí¼ÓÑÕÉ«¡£
²ÊÉ«»¯
Ìí¼ÓÑÕÉ«Ï൱ÈÝÒ×£»µÚÒ»²½ÊÇÉè¼Æ²»´øÑÕÉ«µÄÌáʾÐС£È»ºó£¬ÎÒÃÇËùÒª×öµÄÖ»ÊÇÌí¼ÓÖնˣ¨¶ø²»ÊÇ bash£©¿Éʶ±ðµÄרÓÃתÒåÐòÁУ¬ÒÔʹËüÒÔ²ÊÉ«ÏÔʾÎı¾µÄijЩ²¿·Ö¡£±ê×¼ Linux ÖÕ¶ËºÍ X ÖÕ¶ËÔÊÐíÄúÉèÖÃÇ°¾°£¨ÎÄ×Ö£©ÑÕÉ«ºÍ±³¾°ÑÕÉ«£¬Èç¹ûÐèÒª£¬»¹¿ÉÒÔÆôÓà "bold" ×Ö·û¡£ÓаËÖÖÑÕÉ«¿É¹©ÎÒÃÇÑ¡Ôñ¡£
ÑÕÉ«ÊÇͨ¹ýÔÚ PS1 ÖÐÌí¼ÓרÓÃÐòÁÐÀ´Ñ¡ÔñµÄ -- »ù±¾ÉÏÊǼÐÔÚ "e["£¨×ªÒ忪·½À¨ºÅ£©ºÍ "m" Ö®¼äÊý×ÖÖµ¡£Èç¹ûÖ¸¶¨Ò»¸öÒÔÉϵÄÊý×Ö´úÂ룬ÔòÓ÷ֺŽ«ËüÃÇ·Ö¿ª¡£ÏÂÃæÊÇÒ»¸öÑÕÉ«´úÂëʾÀý£º

"e[0m"

Èç¹û½«Êý×Ö´úÂëÖ¸¶¨ÎªÁ㣬ÔòËü¾Í»á֪ͨÖն˽«Ç°¾°¡¢±³¾°ºÍ¼Ó´ÖÉèÖÃÖØÖÃΪËüÃǵÄĬÈÏÖµ¡£Äú¿ÉÄÜ»áÔÚÔÚÌáʾÐнáÊøʱʹÓÃÕâ¸ö´úÂ룬ÒÔʹÄú¼üÈëµÄÎÄ×Ö³ÉΪ·Ç²ÊÉ«µÄ¡£ÏÖÔÚ£¬ÈÃÎÒÃÇ¿´Ò»ÏÂÕâЩÑÕÉ«´úÂë¡£Çë×¢ÒâÏÂÃæµÄ×¥ÆÁ½á¹û£º
ÑÕÉ«±í

ҪʹÓÃÕâ¸ö±í£¬Ê×ÏÈÇë²éÕÒÄúҪʹÓõÄÑÕÉ«£¬È»ºó²éÕÒ¶ÔÓ¦µÄÇ°¾°±àºÅ (30-37) ºÍ±³¾°±àºÅ (40-47)¡£ÀýÈ磬Èç¹ûÄúϲ»¶ºÚµ×ÂÌ×Ö£¬Ôò¿É½«±àºÅ·Ö±ðÉèΪ 32 ºÍ 40¡£È»ºó´ò¿ªÄúµÄÌáʾÐж¨Òå²¢ÔÚÆäÖÐÌí¼ÓÊʵ±µÄÑÕÉ«´úÂë¡£ÏÂÃæµÄ¶¨Ò壺

export PS1="w> "

±äΪ£º

export PS1="e[32;40mw> "

µ½ÏÖÔÚΪֹ£¬ÌáʾÐо¡¹ÜÒѾ­ºÜ²»´íÁË£¬µ«ÈÔ²»Ì«ÍêÃÀ¡£ÔÚ bash ÏÔʾ³ö¹¤×÷Ŀ¼ÒÔºó£¬ÎÒÃÇÐèҪʹÓà "e[0m" ÐòÁн«ÑÕÉ«ÖØÐÂÉèÖÃΪÕý³£Öµ¡£

export PS1="e[32;40mw> e[0m"

Õâ¸ö¶¨Ò彫ÏÔʾһ¸öƯÁÁµÄÂÌÉ«ÌáʾÐУ¬µ«ÎÒÃÇÈÔÐèÒª×öһЩɨβ¹¤×÷¡£ÎÒÃDz»ÐèÒª°üÀ¨ "40" Õâ¸ö±³¾°ÑÕÉ«ÉèÖã¬ÒòΪËü½«±³¾°ÉèÖÃΪºÚÉ«£¬¶øºÚÉ«ÊÇĬÈÏÑÕÉ«¡£´ËÍ⣬ÂÌÉ«»¹ºÜ°µ£»ÎÒÃÇͨ¹ýÌí¼ÓÒ»¸ö "1" ÑÕÉ«´úÂëÀ´ÐÞÕýÕâ¸öÎÊÌ⣬Õ⽫ÆôÓøüÁÁµÄ¼Ó´ÖÎÄ×Ö¡£³ýÁËÕâ¸öÐÞ¸ÄÖ®Í⣬ÎÒÃÇ»¹ÐèÒª½«È«²¿·Ç´òÓ¡×Ö·ûÓÃרÓÃµÄ bash תÒåÐòÁÐ "[" ºÍ "]" À¨ÆðÀ´¡£ÕâÁ½¸öÐòÁÐ֪ͨ bash£¬±»À¨ÆðÀ´µÄ×Ö·û²»Õ¼ÓÃÐÐÉϵÄÈκοռ䣬ÕâÑù¾Íʹ×Ô¶¯»»ÐÐÄܹ»¼ÌÐøÕý³£¹¤×÷¡£Ã»ÓÐÕâÁ½¸öתÒåÐòÁУ¬¾¡¹ÜÄúÓÐÁËÒ»¸ö·Ç³£Æ¯ÁÁµÄÌáʾÐУ¬µ«ÊÇÈç¹ûÄú¼üÈëµÄÃüÁîÇ¡ºÃµ½´ïÖն˵Ä×îÓҶˣ¬¾Í»áÔì³ÉÏÔʾ»ìÂÒ¡£ÏÂÃæÊÇÎÒÃÇ×îÖÕµÄÌáʾÐУº

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

±ðµ£ÐÄÔÚͬһ¸öÌáʾÐÐÖÐʹÓü¸ÖÖÑÕÉ«£¬¾ÍÏñÏÂÃæÕâÑù£º

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

Xterm ÖеÄÀÖȤ
ÎÒÒÑ˵Ã÷ÁËÈçºÎÔÚÌáʾÐÐÖÐÌí¼ÓÐÅÏ¢ºÍÑÕÉ«£¬µ«Äú»¹¿ÉÒÔ¸ü½øÒ»²½¡£Äú¿ÉÒÔͨ¹ýÔÚÌáʾÐÐÖÐÌí¼ÓרÓôúÂëÀ´Ê¹ X Öնˣ¨Èç rxvt »ò aterm£©µÄ±êÌâÀ¸µÃµ½¶¯Ì¬¸üС£ÄúËùÒª×öµÄÖ»Êǽ«ÏÂÃæµÄÐòÁÐÌí¼Óµ½ÄúµÄ PS1 ÌáʾÐÐÖУº

"e]2;titlebara"

Ö»ÐëÓÃÄúÏ£ÍûÆä³öÏÖÔÚ xterm ±êÌâÀ¸ÖеÄÎÄ×ÖÌæ»»×Ó´® "titlebar" ¼´¿É£¬ÏÖÔÚÒѾ­Ò»ÇоÍÐ÷ÁË£¡²»±ØʹÓþ²Ì¬ÎÄ×Ö£»Äú¿ÉÒÔ½« bash תÒåÐòÁвåÈë±êÌâÀ¸ÖС£Çë²é¿´ÏÂÃæÕâ¸öʾÀý£¬Ëü½«Óû§Ãû¡¢Ö÷»úÃûºÍµ±Ç°¹¤×÷Ŀ¼ÏÔʾÔÚ±êÌâÀ¸ÖУ¬²¢¶¨ÒåÁËÒ»¸ö¼ò¶Ì¡¢Ã÷ÁÁµÄÂÌÉ«ÌáʾÐУº

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

Õâ¾ÍÊÇÎÒÔÚÉÏÃæµÄ×¥ÆÁ½á¹ûÖÐËùÓõÄÄǸöÌáʾÐС£ÎÒϲ»¶Õâ¸öÌáʾÐУ¬ÒòΪËü½«È«²¿ÐÅÏ¢ÏÔʾÔÚ±êÌâÀ¸ÉÏ£¬¶ø²»ÊÇÏÔʾÔÚÖÕ¶ËÉÏ£¬Öն˶ÔÒ»ÐпÉÒÔÏÔʾ¶àÉÙ×Ö·ûÓÐÏÞÖÆ¡£Ë³±ãÌáÒ»¾ä£¬È·±£Óà "[" ºÍ "]" ½«ÄúµÄ±êÌâÀ¸ÐòÁÐÀ¨ÆðÀ´£¨ÒòΪ¾ÍÖն˶øÑÔ£¬Õâ¸öÐòÁÐÊÇ·Ç´òÓ¡ÐòÁУ©¡£½«´óÁ¿ÐÅÏ¢·ÅÔÚ±êÌâÀ¸ÖеÄÎÊÌâÊÇ£¬Èç¹ûÄúʹÓ÷ÇͼÐÎÖնˣ¨Èçϵͳ¿ØÖÆ̨£©£¬Ôò¿´²»µ½ÕâЩÐÅÏ¢¡£ÎªÁ˽â¾öÕâ¸öÎÊÌ⣬¿ÉÒÔÔÚÄúµÄ .bashrc ÖÐÌí¼ÓÒÔϼ¸ÐУº

if [ "$TERM" = "linux" ]
then
#we're on the system console or maybe telnetting in
export PS1="[e[32;1m]u@H > [e[0m]"
else
#we're not on the console, assume an xterm
export PS1="[e]2;u@H wae[32;1m]>[e[0m] "
fi

Õâ¸ö bash Ìõ¼þÓï¾ä½«¸ù¾Ýµ±Ç°µÄÖÕ¶ËÉèÖö¯Ì¬ÉèÖÃÌáʾÐС£ÎªÁË»ñµÃÒ»ÖÂÐÔ£¬ÄúÒ»¶¨Ï£ÍûÅäÖÃÄúµÄ ~/.bash_profile£¬ÒÔ±ãËüÔÚÆô¶¯Ê±ËÑË÷ (source) ÄúµÄ ~/.bashrc¡£È·±£ÄúµÄ ~/.bash_profile ÎļþÖÐÓÐÒÔÏÂÕâÑùÒ»ÐУº

source ~/.bashrc

ÕâÑù£¬ÎÞÂÛÄú¿ªÆôÒ»¸öµÇ¼ shell »¹ÊÇÒ»¸ö·ÇµÇ¼ shell£¬¶¼»á»ñµÃͬÑùµÄÌáʾÐС£
ºÃÁË£¬ÄúÒÑÕÆÎÕÁËÌáʾÐÐħÊõ¡£ÏÖÔÚ¾¡ÇéÏíÊÜһϣ¬ÖÆ×÷Ò»¸öƯÁÁµÄ²ÊÉ«ÌáʾÐаÉ
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ