2

In a non-interactive bash script, these work:

alias hi="echo hi"
shopt -s expand_aliases
hi

alias hi="echo hi"
if [[ 1 ]]; then
    shopt -s expand_aliases
fi
hi

shopt -s expand_aliases
if [[ 1 ]]; then
    alias hi="echo hi"
fi
hi

alias hi="echo hi"
shopt -s expand_aliases
if [[ 1 ]]; then
    hi
fi

But these do not

alias hi="echo hi"
if [[ 1 ]]; then
    shopt -s expand_aliases
    hi #hi: command not found
fi

shopt -s expand_aliases
if [[ 1 ]]; then
    alias hi="echo hi"
    hi #hi: command not found
fi

Can anyone explain why?

Could it be related to this?

Aliases are expanded when a command is read, not when it is executed.

Does this mean entire bash conditionals read before execution?

Community
  • 1
  • 1
jozxyqk
  • 16,424
  • 12
  • 91
  • 180
  • Yes, I think it's related to the link you give; it's because aliases defined inside compound commands (functions, groups, blocks of `if`/`else`, etc) are not available from within this group. That's because Bash parses the whole command, and alias expansions and keywords processing occurs before command execution. And `alias` is a command... – gniourf_gniourf Feb 26 '15 at 07:42
  • 2
    You should consider shell functions instead of aliases anyhow. – tripleee Feb 26 '15 at 17:18

0 Answers0