最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

shell - Are there circumstances in which bash seems to violate the order of precedence for aliases? - Stack Overflow

programmeradmin3浏览0评论

I found the order of precedence described here but if and only if bash is instructed to source a script after the alias is given in .bashrc does the alias-function get used by the sourced script.

E.g.:

function save_cd(){
  cd "$@" 
  echo "$(pwd)" > dummy file 
  echo "$(pwd)"
};
alias cd="save cd";
source 'script-that-calls-cd-in-it'

When I tried sourcing the script that calls cd before declaring cd to be an alias the alias was not"expanded" in the shell environment if I'm using expanded properly here. Instead subsequent calls to sourced_script_defined_function used the cd built-in rather than my alias/function or in my case.. nothing was echoed.

I found the order of precedence described here but if and only if bash is instructed to source a script after the alias is given in .bashrc does the alias-function get used by the sourced script.

E.g.:

function save_cd(){
  cd "$@" 
  echo "$(pwd)" > dummy file 
  echo "$(pwd)"
};
alias cd="save cd";
source 'script-that-calls-cd-in-it'

When I tried sourcing the script that calls cd before declaring cd to be an alias the alias was not"expanded" in the shell environment if I'm using expanded properly here. Instead subsequent calls to sourced_script_defined_function used the cd built-in rather than my alias/function or in my case.. nothing was echoed.

Share Improve this question edited Mar 21 at 19:20 Charles Duffy 297k43 gold badges434 silver badges489 bronze badges asked Mar 21 at 19:00 user377241user377241 831 gold badge1 silver badge7 bronze badges 5
  • 2 Scripts don't expand aliases by default. You need shopt -s expand_aliases to expand them in non-interactive shells. – choroba Commented Mar 21 at 19:06
  • 2 And I think you meant alias cd="save_cd" ? – shellter Commented Mar 21 at 21:45
  • Beyond that, to have a testable minimal reproducible example we'd need to see a specific example of script-that-calls-cd-in-it that doesn't behave as expected in conjunction with your code. If the script is running, say, bash -c 'cd ...', that would explain it not honoring the alias (and also break the function approach, but in a way you can fix with export -f cd to copy the function into the environment for use by subprocesses). – Charles Duffy Commented Mar 21 at 23:58
  • No, keeping the alias defined after the function works as expected. And I really learned something from your point about using the command command or builtin keywords. I am still familiarizing with stackoverflow and how I can best honor their "what to do when someone answers your question" faq guidelines and still benefit as a meet hobbyist. But I'll go ahead and mark yours as the answer. Any way you could address my comment on the answer about adding a resource that describes parse time expansion? Thanks Charles for your time and thought on this..

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论