When I execute the following script
#!/usr/bin/env bash
main() {
shopt -s expand_aliases
alias Hi='echo "Hi from alias"'
Hi # Should Execute the alias
\Hi # Should Execute the function
"Hi"
}
function Hi() {
echo "Hi from function"
}
main "$@"
Very first time it executes the function and then always executes as alias:
$ . Sample.sh
Hi from function
Hi from function
Hi from function
$ . Sample.sh
Hi from alias
Hi from function
Hi from function
Why is it so?
This does not happen in the following case
#!/usr/bin/env bash
function Hi() {
echo "Hi from function"
}
shopt -s expand_aliases
alias Hi='echo "Hi from alias"'
Hi # Should Execute the alias
\Hi # Should Execute the function
"Hi"
Very first time it executes the function and then always executes as alias:
$ . Sample.sh
Hi from alias
Hi from function
Hi from function
$ . Sample.sh
Hi from alias
Hi from function
Hi from function
Best Answer
Alias expansion in a function is done when the function is read, not when the function is executed. The alias definition in the function is executed when the function is executed.
See Alias and functions
and https://www.gnu.org/software/bash/manual/html_node/Aliases.html
This means, the alias will be defined when function
main
is executed, but when the function was read for the first time the alias was not yet defined. So the first time functionmain
will execute functionHi
three times.When you source the script for the second time, the alias is already defined from the previous run and can be expanded when the function definition is read. When you now call the function it is run with the alias expanded.
The different behavior occurs only when the script is sourced with
. Sample.sh
, i.e. when it is run in the same shell several times. When you run it in a separate shell as./Sample.sh
it will always show the behavior of the first run.