我如何从另一个脚本访问位于 .bashrc 文件内的别名

问题描述 投票:0回答:1
  • 这是.bashrc中的代码
#!/bin/bash
alias py="python"
alias psv="python -m http.server"
alias cl="clear"
  • 这是foo.sh
  • 中的代码
#!/bin/bash
git add .
git commit -m "fixed requests error"
git push
cl

最后我得到以下内容

command not found
bash sh
1个回答
0
投票

当 shell 非交互式时(例如在脚本中运行),别名不会展开。从手册页:

当 shell 不具有交互性时,别名不会展开,除非使用 shopt 设置 Expand_aliases shell 选项(请参阅下面的 SHELL BUILTIN COMMANDS 下的 shopt 描述)。

在使用它们之前,您必须添加

shopt -s expand_aliases
,然后在脚本中获取 .bashrc,但您应该更喜欢使用函数,因为它们更灵活,允许您对多个命令进行分组,甚至添加其他参数。

就其价值而言,bash 手册在别名部分的末尾有这样的内容:

对于几乎所有用途,别名都被 shell 函数取代。

© www.soinside.com 2019 - 2024. All rights reserved.