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
shopt -s expand_aliases
to expand them in non-interactive shells. – choroba Commented Mar 21 at 19:06alias cd="save_cd"
? – shellter Commented Mar 21 at 21:45script-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 withexport -f cd
to copy the function into the environment for use by subprocesses). – Charles Duffy Commented Mar 21 at 23:58