在〜/ .bashrc中,我定义了一些别名.但是我不能在其他shell脚本中使用它们,在那里我只能使用那里定义的别名.即使我采购了bashrc,它仍然无效.我该怎么办?
PS.我在打击.
Pau*_*ce. 28
shopt -s expand_aliases除了采购之外,您还需要在脚本中执行此操作~/.bashrc.
Aru*_*gal 12
最简单的答案是做两件重要的事情,否则它不会起作用。
#!/bin/bash -i
# Expand aliases defined in the shell ~/.bashrc
shopt -s expand_aliases
Run Code Online (Sandbox Code Playgroud)
此后,您在 ~/.bashrc 中定义的别名将在您的 shell 脚本(giga.sh 或 any.sh)以及此类脚本中的任何函数或子 shell 中可用。
如果你不这样做,你会得到一个错误:
your_cool_alias: command not found
Run Code Online (Sandbox Code Playgroud)
.bashrc其目的只有一个:为交互式 shell 配置环境。如果您希望在您的.bashrc脚本和其他脚本之间共享代码,那么它属于一个单独的文件,该文件由您的每个.bashrc脚本和 shell 脚本获取。