2010-03-23 13 views
10

bash에서 expand_aliases를 적용 할 수 없습니다. 나는 많은 다른 것들을 시도했지만 아무런 효과가 없습니다.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기능 별칭을 통해을 선호 쉘, 거의 모든 목적을 위해

을 말한다.

위의 마지막 문장 대신 [emphasis mine]입니다. 별칭은 스크립트에서 사용되어야하는 것이 아니라 명령 줄의 편의라고 생각합니다 (한 줄짜리 전용 bash -c 만 포함).

+1

그 중 일부는 심각합니다. LMManPageTFY –

+0

그들은 bash 정보에 다음을 추가해야합니다. "결과적으로 별칭에 대한 함수 정의가 실제로 작동하기 전에 함수에 사용 된 별칭을 정의해야합니다." –

관련 문제