简体   繁体   English

无法在bash脚本的反引号内使用别名

[英]Can't use alias within backticks inside bash script

I have the following script: 我有以下脚本:

for i in 1 2 3 
do
 alias echo${i}="echo ${i}"
 alias aliastest${i}="echo aliastest `echo${i}`"
done

after executing it, I'd expect to have 3 aliases in the names aliastest1, aliastest2, aliastest3. 执行完之后,我希望在名称aliastest1,aliastest2,aliastest3中有3个别名。

However I'm getting: 但是我得到:

./test.sh: line 5: echo1: command not found
./test.sh: line 5: echo2: command not found
./test.sh: line 5: echo3: command not found

Note that when trying to execute echo1, echo2, echo3 from command line, it does work as expected. 请注意,当尝试从命令行执行echo1,echo2,echo3时,它确实按预期工作。

What am I doing wrong? 我究竟做错了什么?

Alias expansion is only enabled for interactive shells by default. 默认情况下,仅对交互式外壳启用别名扩展。 You would need to add 您需要添加

shopt -s expand_aliases

to the beginning of the script. 到脚本的开头。

It's likely that whatever alias you want to define, you should probably be defining as a shell function instead. 无论您要定义什么别名,都可能应该将其定义为Shell函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM