»¹ÓÐÒ»¸öʹ¹¤×÷±äµÃÇáËɵķ½·¨ÊÇʹÓÃÃüÁî±ðÃû¡£ÃüÁî±ðÃûͨ³£ÊÇÆäËûÃüÁîµÄËõд£¬ÓÃÀ´¼õÉÙ¼üÅÌÊäÈë¡£
ÃüÁî¸ñʽΪ:
alias [alias-name=¡¯original-command¡¯]
ÆäÖУ¬alias-nameÊÇÓû§¸øÃüÁîÈ¡µÄ±ðÃû£¬original-commandÊÇÔÀ´µÄÃüÁîºÍ²ÎÊý¡£ÐèҪעÒâµÄÊÇ£¬ÓÉÓÚBashÊÇÒÔ¿Õ¸ñ»òÕ߻سµÀ´Ê¶±ðÔÀ´µÄÃüÁîµÄ£¬ËùÒÔÈç¹û²»Ê¹ÓÃÒýºÅ¾Í¿ÉÄܵ¼ÖÂBashÖ»½ØÈ¡µÚÒ»¸ö×Ö£¬´Ó¶ø³öÏÖ´íÎó¡£Èç¹ûaliasÃüÁîºóÃ治ʹÓÃÈκβÎÊý£¬ÔòÏÔʾµ±Ç°ÕýÔÚʹÓõı»±ðÃû»¯µÄÃüÁî¼°Æä±ðÃû¡£ÎªÃüÁîÈ¡µÄ±ðÃûÔڸôεǼÆÚ¼äʼÖÕÓÐЧ¡£Èç¹ûÓû§ÐèÒª±ðÃûÔÚÿ´ÎµÇ¼ʱ¶¼ÓÐЧ£¬ÄÇô¾Í½«aliasÃüÁîдµ½³õʼ»¯½Å±¾ÎļþÖС£
[Àý]Èç¹û¾³£Òª¼üÈëÈçϵÄÃüÁ×îºÃΪËü½¨Á¢Ò»¸ö±ðÃûÀ´¼õÉÙ¹¤×÷Á¿¡£
$ cd /usr/X11/lib/X11
¼ÙÈçΪÕâ¸ö³¤ÃüÁÁ¢Ò»¸öÃûΪgoconfigµÄ±ðÃû£¬ÔÚBashÌáʾ·ûϼüÈëÈçÏÂÃüÁ
$ alias goconfig=¡¯cd /usr/X11/lib/X11¡¯
ÏÖÔÚ£¬³ý·ÇÄúÍ˳öBash£¬¼üÈëgoconfig½«ºÍÔÀ´µÄ³¤ÃüÁîÓÐͬÑùµÄ×÷Óá£Èç¹ûÏëÈ¡Ïû±ðÃû£¬¿ÉÒÔʹÓÃÏÂÃæµÄÃüÁ
$ unalias goconfig
ÕâÊÇһЩºÜ¶àÈËÈÏΪÓÐÓõıðÃû£¬¿ÉÒÔ°ÑËüÃÇдÈë³õʼ»¯½Å±¾ÎļþÖÐÀ´Ìá¸ß¹¤×÷ЧÂÊ£º
alias ll=¡¯ls �l¡¯
alias log=¡¯logout¡¯
alias ls=¡¯ls �F¡¯
Èç¹ûÄúÊÇÒ»ÃûDOSÓû§²¢ÇÒÏ°¹ßÁËDOSÃüÁ¿ÉÒÔÓÃÏÂÃæµÄ±ðÃû¶¨ÒåʹLinux±íÏÖµÃÏóDOSÒ»Ñù£º
alias dir=¡¯ls¡¯
alias copy=¡¯cp¡¯
alias rename=¡¯mv¡¯
alias md=¡¯mkdir¡¯
alias rd=¡¯rmdir¡¯
×¢Ò⣺ÔÚ¶¨Òå±ðÃûʱ£¬µÈºÅÁ½±ß²»ÄÜÓпոñ£¬·ñÔòshell²»Äܾö¶¨ÄúÐèÒª×öʲô¡£½öÔÚÃüÁîÖаüº¬¿Õ¸ñ»òÌØÊâ×Ö·ûʱ²ÅÐèÒªÒýºÅ¡£
Èç¹û¼üÈë²»´øÈκβÎÊýµÄaliasÃüÁ½«ÏÔʾËùÓÐÒѶ¨ÒåµÄ±ðÃû¡£
Ìáʾ·û
BashÓÐÁ½¼¶Ìáʾ·û¡£µÚÒ»¼¶Ìáʾ·ûÊǾ³£¼ûµ½µÄBashÔڵȴýÃüÁîÊäÈëʱµÄÇé¿ö¡£µÚÒ»¼¶Ìáʾ·ûµÄĬÈÏÖµÊÇ$·ûºÅ¡£Èç¹ûÓû§²»Ï²»¶Õâ¸ö·ûºÅ£¬»òÕßÔ¸Òâ×Ô¼º¶¨ÒåÌáʾ·û£¬Ö»ÐèÐÞ¸ÄPS1±äÁ¿µÄÖµ¡£ÀýÈ罫Æä¸ÄΪ£º
PS1=¡±Enter a command:¡±
µÚ¶þ¼¶Ìáʾ·ûÊǵ±BashΪִÐÐijÌõÃüÁîÐèÒªÓû§ÊäÈë¸ü¶àÐÅϢʱÏÔʾµÄ¡£µÚ¶þ¼¶Ìáʾ·ûĬÈÏΪ¡µ¡£Èç¹ûÐèÒª×Ô¼º¶¨Òå¸ÃÌáʾ·û£¬Ö»Ðè¸Ä±äPS2±äÁ¿µÄÖµ¡£ÀýÈ罫Æä¸ÄΪ£º
PS2=¡±More information:¡±
ÉÏÃæµÄÁ½¸öÀý×Ó¶¼ÊÇÉ趨Ìáʾ·ûΪ¾²Ì¬×Ö·û´®µÄÇé¿ö¡£ÆäʵÓû§Ò²¿ÉÒÔʹÓÃһЩÊÂÏÈÒѾ¶¨ÒåºÃµÄÌØÊâ×Ö·û¡£ÕâЩÌØÊâ×Ö·û½«Ê¹Ìáʾ·ûÖаüº¬µ±Ç°Ê±¼äÖ®ÀàµÄÐÅÏ¢¡£±í10-4ÁгöÁË×î³£ÓõÄһЩÌØÊâ×Ö·û¼°Æ京Òå¡£
±í10-4¡¡bashÌáʾ·û³£ÓÃÌØÊâ×Ö·û
ÌØÊâ×Ö·û
˵ Ã÷
!
ÏÔʾ¸ÃÃüÁîµÄÀúÊ·±àºÅ
#
ÏÔʾshell¼¤»îºó£¬µ±Ç°ÃüÁîµÄÀúÊ·±àºÅ
$
ÏÔʾһ¸ö$·ûºÅ£¬Èç¹ûµ±Ç°Óû§ÊÇrootÔòÏÔʾ#·ûºÅ
ÏÔʾһ¸ö·´Ð±¸Ü
d
ÏÔʾµ±Ç°ÈÕÆÚ
h
ÏÔʾÔËÐиÃshellµÄ¼ÆËã»úÖ÷»úÃû
n
´òÓ¡Ò»¸ö»»Ðзû£¬Õ⽫µ¼ÖÂÌáʾ·û¿çÐÐ
s
ÏÔʾÕýÔÚÔËÐеÄShellµÄÃû³Æ
t
ÏÔʾµ±Ç°Ê±¼ä
u
ÏÔʾµ±Ç°Óû§µÄÓû§Ãû
W
ÏÔʾµ±Ç°¹¤×÷Ŀ¼»ù×¼Ãû
w
ÏÔʾµ±Ç°¹¤×÷Ŀ¼
ÕâЩÌØÊâ×Ö·û¿ÉÒÔ×éºÏÆðÀ´£¬ÎªÓû§ÌṩһЩÌáʾ·û£¬ÌṩºÜÓÐÓõÄÐÅÏ¢¡£ÏÂÃæÀ´¿´¼¸¸öʵ¼ÊÀý×Ó£º
PS1=¡±t¡±
½«Ê¹Ìáʾ·û±ä³ÉÈçÏÂËùʾ£º
02:16:15
¶ø PS1=t
½«Ê¹Ìáʾ·û±ä³ÉÈçÏÂËùʾ£º
t
ÈôPS1=¡±t¡±
½«Ê¹Ìáʾ·û±ä³ÉÈçÏÂËùʾ£º
02:16:30
¸ÃÀý¾ÍÊÇʹÓÃÁ½¸öÌØÊâ×Ö·ûµÄ×éºÏµÃµ½µÄ¡£
¿ØÖÆshellµÄÔËÐз½Ê½
BashÓÐһЩÌØÊâ±äÁ¿£¬ÄÜ¿ØÖÆshellÒÔ²»Í¬µÄ·½Ê½¹¤×÷¡£ÀýÈ磬±äÁ¿noclobberÄÜ·ÀÖ¹ÔÚÖض¨ÏòÊä³öʱÒâÍâµØ¸²¸ÇÒ»¸öÎļþ¡£Í¨¹ý setÃüÁî¿ÉÒÔÉèÖÃnoclobber±äÁ¿µÄÓÐЧ»òÎÞЧ¡£setÃüÁîÓÐÁ½¸ö²ÎÊý£ºÒ»¸öÊÇÖ¸¶¨±äÁ¿¿ª£¨on£©»ò¹Ø£¨off£©µÄÑ¡Ïһ¸öÊÇÌØÊâ±äÁ¿µÄ±äÁ¿Ãû¡£ÒªÊ¹Ä³Ò»ÌØÊâ±äÁ¿¿ª£¨ÓÐЧ£©£¬ÓÃ-oÑ¡ÏҪʹÆä¹Ø£¨ÎÞЧ£©£¬ÓÃ+oÑ¡Ïî¡£ÀýÈ磺
$ set �o noclobber // ʹnoclobber±äÁ¿¿ª
$ set +o noclobber // ʹnoclobber±äÁ¿¹Ø
Èý¸ö×î³£ÓõÄshellÌØÊâ±äÁ¿ÓУºignoreeof¡¢noclobber¼°noglob¡£
ignoreeof
ignoreeof±äÁ¿ÓÃÀ´½ûֹʹÓÃctrl+dÀ´Í˳öshell£¨ctrl+d²»½öÓÃÀ´Í˳öshell£¬¶øÇÒ¿ÉÒÔÖÕÖ¹Óû§Ö±½ÓÊäÍù±ê×¼Êä³öÉϵÄÊäÈë¡£¸Ã²Ù×÷¾³£ÔÚһЩshellʵÓÃÃüÁîÖÐʹÓã¬ÀýÈçʵÓÃÃüÁîcat¡£ÔÚÕâЩʵÓóÌÐò²Ù×÷ÖУ¬·Ç³£ÈÝÒ×Îó²Ù×÷¶øÒâÍâµØÍ˳öshell¡£ ignoreeofÌØÊâ±äÁ¿ÕýÊÇÓÃÀ´·ÀÖ¹ÕâÖÖÒâÍâµÄÍ˳ö¡£ÀýÈ磺
$ set �o ignoreeof
Ö®ºó£¬Óû§Ö»ÄÜÓÃlogout»òexitÃüÁîÍ˳öshell¡£
noclobber
noclobber±äÁ¿¿ÉÒÔÔÚÖض¨ÏòÊä³öʱ±£»¤ÒÑ´æÔÚµÄÎļþ£¬·ÀÖ¹±»ÒâÍâµØ¸²¸Ç¡£ÔÚÏÂÀýÖУ¬Óû§ÉèÖÃnoclobberΪÓÐЧ£¬ÔÚÖض¨Ïòʱ£¬Óû§ÊÔͼȥ¸²¸ÇÒѾ´æÔÚµÄÎļþmyfile£¬´Ëʱϵͳ½«·µ»ØÒ»¸ö´íÎóÐÅÏ¢¡£
[Àý]
$ set �o noclobber
$ cat preface>myfile
bash: myfile: cannot overwrite existing file
$
noglob
ÉèÖÃnoglob±äÁ¿ºó£¬shell½«²»À©Õ¹ÎļþÃûÖÐһЩÌØÊâµÄ×Ö·û»ò×Ö·û´®¡£Èç×Ö·û*¡¢?¡¢ ]µÈ½«²»ÔÙ×÷ΪͨÅä·û¡£Èç¹ûÓû§Ï£ÍûÁгö½áβΪ?µÄÎļþÃûanswer?£¬¿Éͨ¹ýÈçϲ½Ö裺Ê×ÏÈ£¬Óû§Ê¹noglob±äÁ¿ÎªÎÞЧ£¬È»ºóÔÙÁгöÎļþÃû¡£¿ÉÒÔ¿´µ½£¬Ä¿Ç°ÃüÁîÐÐÉϵÄÎʺÅ?±»ÈÏΪÊÇÎļþÃûÖеÄÒ»¸ö×Ö·û£¬¶ø²»ÔÙ±»¿´×÷ͨÅä·û¡£
$ set �o noglob
$ ls answer?
answer?
×ÓshellÓëexportÃüÁî
Óû§µÇ¼µ½Linuxϵͳºó£¬ÏµÍ³½«Æô¶¯Ò»¸öÓû§shell¡£ÔÚÕâ¸öshellÖУ¬¿ÉÒÔʹÓÃshellÃüÁî»òÉùÃ÷±äÁ¿£¬Ò²¿ÉÒÔ´´½¨²¢ÔËÐÐ shell½Å±¾³ÌÐò¡£ÔËÐÐshell½Å±¾³ÌÐòʱ£¬ÏµÍ³½«´´½¨Ò»¸ö×Óshell¡£´Ëʱ£¬ÏµÍ³Öн«ÓÐÁ½¸öshell£¬Ò»¸öÊǵǼʱϵͳÆô¶¯µÄshell£¬ÁíÒ»¸öÊÇϵͳΪÔËÐнű¾³ÌÐò´´½¨µÄshell¡£µ±Ò»¸ö½Å±¾³ÌÐòÔËÐÐÍê±Ï£¬ËüµÄ½Å±¾shell½«ÖÕÖ¹£¬¿ÉÒÔ·µ»Øµ½Ö´Ðиýű¾Ö®Ç°µÄshell¡£´ÓÕâÖÖÒâÒåÉÏÀ´Ëµ£¬Óû§¿ÉÒÔÓÐÐí¶àshell£¬Ã¿¸öshell¶¼ÊÇÓÉij¸öshell£¨³ÆΪ¸¸shell£©ÅÉÉúµÄ¡£
ÔÚ×ÓshellÖж¨ÒåµÄ±äÁ¿Ö»ÔÚ¸Ã×ÓshellÄÚÓÐЧ¡£Èç¹ûÔÚÒ»¸öshell½Å±¾³ÌÐòÖж¨ÒåÁËÒ»¸ö±äÁ¿£¬µ±¸Ã½Å±¾³ÌÐòÔËÐÐʱ£¬Õâ¸ö¶¨ÒåµÄ±äÁ¿Ö»ÊǸýű¾³ÌÐòÄÚµÄÒ»¸ö¾Ö²¿±äÁ¿£¬ÆäËûµÄshell²»ÄÜÒýÓÃËü£¬ÒªÊ¹Ä³¸ö±äÁ¿µÄÖµ¿ÉÒÔÔÚÆäËûshellÖб»¸Ä±ä£¬¿ÉÒÔʹÓÃexportÃüÁî¶ÔÒѶ¨ÒåµÄ±äÁ¿½øÐÐÊä³ö¡£exportÃüÁʹϵͳÔÚ´´½¨Ã¿Ò»¸öеÄshellʱ¶¨ÒåÕâ¸ö±äÁ¿µÄÒ»¸ö¿½±´¡£Õâ¸ö¹ý³Ì³Æ֮Ϊ±äÁ¿Êä³ö¡£
[Àý]ÔÚ±¾ÀýÖУ¬±äÁ¿myfileÊÇÔÚdispfile½Å±¾³ÌÐòÖж¨ÒåµÄ¡£È»ºóÓÃexportÃüÁ±äÁ¿myfileÊä³öÖÁÈκÎ×Óshell£¬ÀýÈçµ±Ö´ÐÐprintfile½Å±¾³ÌÐòʱ²úÉúµÄ×Óshell¡£
dispfile½Å±¾³ÌÐòÇåµ¥£º
/**************begin dispfile**************/
myfile=¡±List¡±
export myfile
echo ¡°Displaying $myfile¡±
pr �t �n $myfile
printfile
/**************end dispfile***************/
¡¡
printfile½Å±¾³ÌÐòÇåµ¥£º
/**************begin printfile**************/
echo ¡°Printing $myfile¡±
lpr $myfile&
/**************end printfile**************/
$dispfile
Displaying List
1 screen
2 modem
3 paper
Printing List
$
¶¨ÖÆBash
ÔÚ±¾½ÚÖÐÒѾ½éÉÜÁ˺ܶඨÖÆBashµÄ·½·¨£¬µ«ÊÇÆù½ñΪֹ£¬ÕâЩ·½·¨¶¼Ö»ÊǶԵ±Ç°Bash¶Ô»°ÓÐÓá£Ö»ÒªÓû§Í˳öµÇ¼£¬Ëù×öµÄÒ»Çиı䶼»á¶ªÊ§¡£ËùÒÔÓ¦¸ÃÔÚBashµÄ³õʼ»¯ÎļþÖÐ×öÓÀ¾ÃÐÔµÄÐ޸ġ£
Óû§¿ÉÒÔ½«Ã¿´ÎÆô¶¯BashËùÐèÒªÖ´ÐеÄÃüÁî·ÅÈë³õʼ»¯ÎļþÖУ¬×î³£¼ûµÄÃüÁî¾ÍÊÇaliasÃüÁîºÍ±äÁ¿¶¨ÒåÁ½ÖÖ¡£ÏµÍ³ÖеÄÿ¸öÓû§ÔÚÆäÖ÷Ŀ¼Öж¼ÓÐÒ»¸ö.bash_profileÎļþ£¬Bashÿ´ÎÆô¶¯Ê±¶¼½«¶ÁÈ¡¸ÃÎļþ£¬ÆäÖаüº¬µÄËùÓÐÃüÁ½«±»Ö´ÐС£
ÏÂÃæ±ãÊÇĬÈÏ.bash_profileÎļþµÄ´úÂ룺
#.bash_profile
#Get the aliases and functions
if [-f ~/.bashrc ];then
.~/.bashrc
fi
#User specific environment and startup programs
PATH=$PATH: $HOME/bin
ENV=$HOME/.bashrc
USERNAME=¡±¡±
Export USERNAME ENV PATH