Aliases in non-interactive bash [duplicate]

安稳与你 提交于 2021-01-27 20:12:14

问题


I would like to get aliases to work in non-interactive bash. I run the following command :

bash -c "alias toto=ls; shopt -s expand_aliases; alias toto=ls; toto"

and I get the following :

bash: toto : commande not found

What am I doing wrong ?


回答1:


From man bash:

Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read.

That means, even in an interactive shell,

alias toto=ls; toto

wouldn't work. There must be a line break between alias definition and call. So,

bash -c 'shopt -s expand_aliases; alias toto=ls
toto'

should work.



来源:https://stackoverflow.com/questions/57605902/aliases-in-non-interactive-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!