Great question, Benjamin! The behavior you're observing is completely normal in Bash, and here's why:
if blockAliases are not expanded in non-interactive shells by default.
When you run a script or a block of code (like inside an if statement), Bash does not expand aliases unless you explicitly enable it with shopt -s expand_aliases.
Aliases are shell-specific.
Even if you enable alias expansion, the alias my_test is only available in the current shell instance where it was defined. If the if block is run in a subshell (which is not the case here, but it's a common point of confusion), the alias would not persist outside of it.
If you want to use an alias inside a script or a block, you need to enable alias expansion at the start of your script or block:
bashshopt -s expand_aliases if true then alias my_test='echo test' my_test fi
Or, more simply, use a function instead of an alias for scripts:
bashif true then my_test() { echo test; } my_test fi
Functions are expanded in non-interactive shells and are generally more reliable for scripting.
shopt -s expand_aliasesWould you like to explore more about Bash scripting or aliases?
This Chat is read-only. Login to resume chatting.
shopt -s expand_aliases
if true
then
alias my_test='echo test'
my_test
fiif true
then
my_test() { echo test; }
my_test
fi