是否可以在 bash 中获取文件,但跳过特定功能?

Som*_*DOS 7 shell bash function

假设我有bash_functions.sh

function test(){
}

function test2(){
}
Run Code Online (Sandbox Code Playgroud)

而在我的~/.bashrc我做:

source ~/bash_functions.sh
Run Code Online (Sandbox Code Playgroud)

在采购时是否可以避免采购特定功能?我的意思是,bash_functions.sh除了test?

Gil*_*il' 4

在函数定义中foo () { \xe2\x80\xa6 },如果foo是别名,则将其展开。有时这可能是一个问题,但在这里它有帮助。在获取文件之前使用foo其他名称的别名,您将定义不同的函数。在 bash 中,别名扩展在非交互式 shell 中默认处于关闭状态,因此您需要使用shopt -s expand_aliases.

\n\n

如果sourced.sh包含

\n\n
foo () {\n  echo "foo from sourced.sh"\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

那么你就这样使用它

\n\n
foo () {\n  echo "old foo"\n}\nshopt -s expand_aliases   # necessary in bash; just skip in ash/ksh/zsh\nalias foo=do_not_define_foo\n. sourced.sh\nunalias foo; unset -f do_not_define_foo\nfoo\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后你就得到了old foo。请注意,源文件必须使用foo () { \xe2\x80\xa6 }函数定义语法,而不是function foo { \xe2\x80\xa6 },因为function关键字会阻止别名扩展。

\n