我正在尝试编写一个脚本来为我的 bash shell 设置别名,但我不想source
自动输入.bashrc
- 我需要在我的终端子集中使用别名。
是否可以alias
在脚本中使用命令并使别名命令适用于运行脚本的 shell?
期望的功能:
$ alias
# ... no output here
$ ./my-script
$ alias
alias foo='bar'
alias alpha='beta'
...
Run Code Online (Sandbox Code Playgroud)
别名对于创建它们的外壳是私有的。它们既不能导出,也不能从父外壳访问。
最简单的解决方案是按照您的建议将别名分解为一个单独的文件,然后手动获取该文件,或者向您的 中添加一个函数.bashrc
,该函数将在调用时获取它们。
function extra-aliases {
. /path/to/file/containing/additional/aliases
}
Run Code Online (Sandbox Code Playgroud)