I work on Ubuntu and using bash shell
I am trying to execute 'aliases' as shown below but it does not work
When I try 'shopt -s expanded_aliases; source env.sh; hal-on' outside function it works but inside a function it does not work
do_build()
{
source env.sh
shopt -s expand_aliases
hal-on
}
# Main
do_build
Error Message
- hal-on ./temp: line 10: hal-on: command not found
What am I doing wrong?
Aliases have weird behaviour in bash and it is almost always better to use a function. What you have in your example is likely a definition of aliases within a function and I'm not sure what happens there.
Also, it's uncommon and problematic to source files within a function, unless its just a bunch of variable sets. In fact, unless you're explicitly declaring the variables to be "local", there's little difference to doing the source outside.
One case I can see that you might be in is to only do the things defined in the environment in a certain condition. and if your variable sets are complex I can see you not wanting to do that. But really restrict it to variable sets... no function/alias definitions.
Read up on aliases in the bash man page. Search for "Aliases are not expanded". It has something to do with "aliases are expanded at read time". With some experimentation, it appears that aliases are not expanded within a function even if the shopt is set before the function definition.
So... use functions instead.
alias foo="bar baz"
becomesfoo(){ bar baz "$@" ;}
and generally operates identically.