Why aliases in a non-interactive Bash shell do not work

旧巷老猫 提交于 2019-11-27 00:55:48

The command shopt -s expand_aliases will allow alias expansion in non-interactive shells.

.bashrc is only processed by interactive shells.

In addition, aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt. Unless, of course, POSIX mode is invoked by calling the shell with the name sh instead of bash.

People who use aliases a lot often source their .bashrc at the end of their profile so that the aliases are there even for non-interactive shells. This might not be the best way, but it is pretty common.

It's things like this that lead me to believe that in the 21st century we should abandon shell scripts in favor of a full-blown language like Python. It's a lot more predictable.

You have to

shopt -s expand_aliases

in the file pointed to in your BASH_ENV

I had similar issue, in the end, I found out that ~/.bashrc was all I needed.

However, in Ubuntu, I had to comment the line that stops processing ~/.bashrc :

If not running interactively, don't do anything
[ -z "$PS1" ] && return
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!