The problem is the pipeline. In a pipeline of the form a | b | c a | b | c a | b | c , each of the individual commands a , b , and c is run in a separate subshell [ ref ], which means that it receives a copy of the parent execution environment (including aliases), and any changes it makes to its own copy ( such as by running alias ) will have no effect on the parent [ ref ].
In your case, you can fix this by writing:
while read x; do eval "alias $x='echo foo'" done < <(echo -e "a3\na4")
which will still run echo -e "a3\na4" in the subshell, but will run while -loop in the normal / parent runtime.
ruakh source share