2010-03-23 17 views
10

मुझे विस्तार में पूर्वाग्रहों को प्रभावी बनाने के लिए नहीं मिल सकता है। मैंने कई अलग-अलग चीजों की कोशिश की है, और कुछ भी काम नहीं करता है।विस्तार करने के लिए expand_aliases नहीं मिल सकता

/bin/bash -c 'shopt -s expand_aliases; alias cdtmp="cd /tmp"; alias; cdtmp; pwd;' 

और उत्पादन: सिर्फ साबित करने के लिए

$ /bin/bash -c 'shopt -s expand_aliases; alias cdtmp="cd /tmp"; alias; cdtmp; pwd;' 
alias cdtmp='cd /tmp' 
/bin/bash: cdtmp: command not found 
/home/user 

$ /bin/bash --version 
GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu) 
Copyright (C) 2005 Free Software Foundation, Inc. 

(हाँ, मैं -O विकल्प के बजाय shopt उपयोग कर रहा हूँ पार्टी की योजना बनाई है, यह किया जा रहा है

यहाँ साधारण परीक्षण मामला है किया गया।)

कोई विचार?

+0

क्या आपने यह देखने के लिए 'shopt -p expand_aliases' की कोशिश की है कि यह वास्तव में सक्षम है या नहीं? – Chris

+0

हां, और यह वापस आ गया। डेनिस ने इसे मिला; जब मैंने उपरोक्त को एक शेल स्क्रिप्ट में सहेजा और इसे चलाया, तो यह सही ढंग से काम करता था। – sachmet

उत्तर

11

उपनाम एक ही पंक्ति पर या उसी कार्य में उपलब्ध नहीं हैं जहां उन्हें परिभाषित किया गया है।

बैश आदमी पृष्ठ से:

 
     The rules concerning the definition and use of aliases are somewhat 
     confusing. Bash always reads at least one complete line of input 
     before executing any of the commands on that line. Aliases are 
     expanded when a command is read, not when it is executed. Therefore, 
     an alias definition appearing on the same line as another command does 
     not take effect until the next line of input is read. The commands 
     following the alias definition on that line are not affected by the new 
     alias. This behavior is also an issue when functions are executed. 
     Aliases are expanded when a function definition is read, not when the 
     function is executed, because a function definition is itself a com‐ 
     pound command. As a consequence, aliases defined in a function are not 
     available until after that function is executed. To be safe, always 
     put alias definitions on a separate line, and do not use alias in com‐ 
     pound commands. 

     For almost every purpose, aliases are superseded by shell functions. 

Bash Reference Manual

का कहना है लगभग हर प्रयोजन के लिए, शैल कार्यों पसंद किया जाता है उपनाम से अधिक।

ऊपर दिए गए अंतिम वाक्य के बजाय [जोर मेरा]। मैं उपनामों को स्क्रिप्ट में उपयोग किए जाने वाले किसी चीज की बजाय कमांड लाइन सुविधा मानता हूं (जिनमें से केवल bash -c एक-लाइनर शामिल हैं)।

+1

यह कुछ गंभीर LMManPageTFY –

+0

है, उन्हें इसे जानकारी को बाश करने के लिए भी जोड़ना चाहिए: "और इसके परिणामस्वरूप, कार्यों में उपयोग किए गए उपनामों को परिभाषित किया जाना चाहिए कि वास्तव में उपनामों को कार्य परिभाषाओं से पहले परिभाषित किया जाना चाहिए।" –

संबंधित मुद्दे