Bash ÖÐµÄ ${} ºÍ $() ÓÐʲôÇø±ð
Ïñ Linux ÕâÑùµÄ»ùÓÚ GNU µÄ²Ù×÷ϵͳÒÀÀµÓÚÒ»¸öÃûΪ Bash µÄÏÂÁîÓïÑÔÚ¹ÊÍÆ÷»ò Shell À´Íê³ÉËüÃǵĴ󲿷ÖÅÌËãʹÃüºÍÄ¿µÄ¡£
Bash ÊÇ Bourne Again Shell µÄËõд£¬Bunne Again Shell ÊÇÒ»¸öË«¹ØµÄÃû³Æ£¬ÊÇ¶Ô Bourne Shell (¼´ Steven Bourne ·¢Ã÷)µÄÖ¾´¡£ÔÚ¼æÈÝÐÔ·½Ã棬Bash ºÍ sh ÊǾøÅä¡£C shell ( csh ) ºÍKorn shell ( ksh ) ÖÐÏÔ×ŵÄÓÐÓÃÌØÕ÷Ò²°üÀ¨ÔÚÕâ¸ö shell ÓïÑÔÚ¹ÊÍÆ÷ÖС£
ÔÚʵÏÖ·½Ã棬Bash Ö¼ÔÚÇÐºÏ IEEE POSIX ¹æ·¶£¨IEEE ±ê×¼ 1003.1£©¹¤¾ß²¿·ÖºÍ IEEE POSIX Shell¡£Òò´Ë£¬×÷ΪһÖÖ½»»¥Ê½ºÍ±à³Ì¹¤¾ß£¬Bash ÌṩµÄ¹¦Ð§Ë¢ÐÂʤ¹ý sh ÖеÄˢС£
Linux ºÍÆäËû»ùÓÚ GNU µÄ²Ù×÷ϵͳµÄÓû§²»±Ø½ö½öÒÀÀµBash×÷ΪËûÃǵÄÖ÷ÒªÏÂÁîÓïÑÔÚ¹ÊÍÆ÷£¬ÓÉÓÚÏñ csh ÕâÑùµÄ shell °æ±¾Ò²Ô¤ÏÈ´ò°üÔÚÕâЩϵͳÖС£ÓÉÓÚÆäÆæÒìµÄ shell ÊôÐÔ£¨Èç¿ÉÒÆÖ²ÐÔ£©£¬Bash ½öÉèÖÃΪÓÃ×÷ĬÈÏ Linux shell¡£
ÔÚ Bash ÖнÏÁ¿ $() Óë ${}
ΪÁ˽ÏÁ¿Óë Bash ÇéÐÎÏà¹ØµÄÕâÁ½¸öÊôÐÔ£¬ÎÒÃDZØÐèÖØÉê$()to$(command)ºÍ${}to ${parameter}¡£Òò´Ë£¬ÎÒÃÇ¿ÉÒÔʵÑé½çËÃ÷ÈÕâÁ½¸ö Bash ʵÌå¡£
Ã÷È· $(ÏÂÁî)
$(command)ÔÚBashÖÐÉæ¼°ÏÂÁîÌæ»»¡£¸üÏêϸµØ˵£¬ÏÂÁ·Öin$(command)±»Ö´ÐУ¬ÆäÊä³ö±»²¶»ñ²¢´òÓ¡ÔÚ±ê×¼Êä³öÉÏ¡£
Å£±Æ°¡£¡½Ó˽»î±Ø±¸µÄ N ¸ö¿ªÔ´ÏîÄ¿£¡¸ÏæÕä²Ø
µÇ¼ºó¸´ÖÆ
±³ºóµÄÓï·¨$(command)ÊÇÁîÅƼ¶±ðµÄ¡£Ë¼Á¿Ö´ÐÐÒÔÏÂÏÂÁ¸ÃÏÂÁîÖ¼ÔÚÔÚÖÕ¶ËÉÏ´òÓ¡³öÄ¿½ñÈÕÆÚ¡£
echo "½ñÌìÊÇ $(date). LinuxMi.com ÓÖÊÇÓÅÃÀµÄÒ»Ìì¡£"
µÇ¼ºó¸´ÖÆ
ÔÚ Linux ÖдòÓ¡ÈÕÆÚ
ÉÏÊöÖ´ÐпÉÒÔÓÃÒÔÏ·½·¨Ú¹ÊÍ¡£Ê×ÏÈ£¬Ö´ÐÐ date ÏÂÁî²¢½«ÆäÊä³ö×÷Ϊ²ÎÊý°üÀ¨ÔÚ echo ÏÂÁîÖС£
ÔÚÕâÀÎÒÃÇ¿ÉÒÔ¼ÙÉèÏÂÁîÌæ»»ÔÚÖØаüÀ¨ÔÚÖ÷ shell ֮ǰÔÚµ¥¶ÀµÄ×Ó shell ÖÐÖ´ÐС£
Ïàʶ ${parameter}
${parameter}ÔÚBashÖÐÉæ¼°²ÎÊýÌæ»»¡£Æ¾Ö¤ Bash ÊÖÊéÒ³£¬²ÎÊýÊÇÒ»¸öÖµ´æ´¢ÊµÌå¡£²ÎÊýʵÌå¿ÉÒÔÓëÃû³Æ¡¢Êý×Ö»òÌØÊâ×Ö·ûÏà¹ØÁª¡£
´óÀ¨ºÅ{}ÌåÏÖÎÒÃÇÔÚ´¦ÀíλÖòÎÊý£¬»òÕßÔÚʹÓõIJÎÊý£¨²ÎÊýÖµ£©ºóÃæÊÇÒ»¸öÓë²ÎÊýÃû³Æ²»Ïà¹ØµÄ·ÇÚ¹ÊÍÐÔ×Ö·û£¬ÀýÈç${animal}s¡£
ÖÁÓÚλÖòÎÊý£¬¿ÉÒÔÓÃÒ»¸ö»ò¶à¸öÊý×ÖÀ´ÌåÏÖ£¨µ¥¸öÊý×Ö 0 ³ýÍ⣩¡£Òò´Ë£¬ÈôÊÇÎÒÃÇÓÐÒ»¸öÃûΪ$variable_nameµÄ±äÁ¿£¬Ôò¿ÉÒÔʹÓòÎÊý Ìæ»»${parameter}À´¼ìË÷¹ØÁªµÄ±äÁ¿Öµ¡£
˼Á¿ÒÔϱäÁ¿ÉùÃ÷ÏÂÁ
$ animal=lion
µÇ¼ºó¸´ÖÆ
ÈÃÎÒÃÇŲÓà animal ±äÁ¿ÒÔʵÑ齫±äÁ¿Öµ¸´Êý£º
$ echo $animals
µÇ¼ºó¸´ÖÆ
ÔÚÏÂÁîÖÐÉùÃ÷±äÁ¿
ÔÚ±ê×¼Êä³öÖв»»á´òÓ¡ÈκÎÄÚÈÝ£¬ÓÉÓÚ Bash ²»Ã÷È·±äÁ¿$animals¡£¿ÉÊÇ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÈçÏ·½·¨Ê¹ÓòÎÊýÌæ»»ÒªÁìÀÖ³ÉŲÓÃÎÒÃǵÄ$animals±äÁ¿:
$ echo ${animal}s
µÇ¼ºó¸´ÖÆ
ÔÚÏÂÁîÖÐŲÓñäÁ¿
ÔÚijЩÇéÐÎÏ£¬Ì¾Ï¢ºÅ¿ÉÄÜ»áÔÚ²ÎÊýÌ滻ʵÏֵĵÚÒ»¸ö×ó´óÀ¨ºÅÖ®ºó·ºÆð¡£ÔÚÕâÖÖÇéÐÎÏ£¬¾Í»á·ºÆð±äÁ¿¼ä½ÓµÄ¿´·¨¡£ÁíÍ⣬ËÑË÷ÃñÖÚºÅLinux¾Í¸ÃÕâÑùѧºǫ́»Ø¸´¡°ºï×Ó¡±£¬»ñÈ¡Ò»·Ý¾ªÏ²Àñ°ü¡£
±äÁ¿ÖµÓÅÏÈÓÚ²ÎÊýÖµ£¬ÈçÏÂËùʾ£º
$ animal=lion $ echo $animal $ lion=rafiki $ echo $lion $ echo ${!animal}
µÇ¼ºó¸´ÖÆ
²ÎÊýÖµµÄÓÅÏȼ¶
Ï£Íû±¾ÎÄÄܹ»ËµÃ÷ Bash ÖÐ$()ºÍ${}Ö®¼äµÄÇø±ð¡£
ÒÔÉϾÍÊÇBash ÖÐµÄ ${} ºÍ $() ÓÐʲôÇø±ðµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡