无法在 bash 脚本内的反引号内使用别名 [英] Can't use alias within backticks inside bash script

查看:71
本文介绍了无法在 bash 脚本内的反引号内使用别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本:

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

执行它后,我希望在名称aliastest1,aliastest2,aliastest3中有3个别名.

after executing it, I'd expect to have 3 aliases in the names aliastest1, aliastest2, aliastest3.

但是我得到了:

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

请注意,当尝试从命令行执行echo1,echo2,echo3时,它确实可以正常工作.

Note that when trying to execute echo1, echo2, echo3 from command line, it does work as expected.

我在做什么错了?

推荐答案

默认情况下,仅对交互式shell启用别名扩展.您需要添加

Alias expansion is only enabled for interactive shells by default. You would need to add

shopt -s expand_aliases

到脚本的开头.

很可能无论您要定义什么别名,都应该将其定义为Shell函数.

It's likely that whatever alias you want to define, you should probably be defining as a shell function instead.

这篇关于无法在 bash 脚本内的反引号内使用别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆