Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An alias defined in a bash script does not work within the same script [duplicate]

Tags:

alias

bash

posix

I have a simple bash script.

alias myls=ls
myls

If I execute this script, I get an error.

$ bash foo.sh 
foo.sh: line 2: myls: command not found
  • Why does the alias not work in the script?
  • Does this behavior conform to POSIX?
  • If it is indeed not supposed to work, could you please point me to an authoritative documentation that stays this?
like image 553
Lone Learner Avatar asked Mar 18 '26 00:03

Lone Learner


1 Answers

See man bash:

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt

like image 50
choroba Avatar answered Mar 19 '26 13:03

choroba