bash 如何从 shell 调用 .bashrc 中定义的函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1500499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you call a function defined in .bashrc from the shell?
提问by les2
In my .bashrc, I have a function called hello:
在我的 .bashrc 中,我有一个名为 hello 的函数:
function hello() {
echo "Hello, !"
}
I want to be able to invoke hello() from the shell as follows:
我希望能够从 shell 调用 hello() 如下:
$ hello Lloyd
And get the output:
并得到输出:
> Hello, Lloyd!
What's the trick?
有什么诀窍?
(The real function I have in mind is more complicated, of course.)
(当然,我想到的真正功能更复杂。)
EDIT: This is REALLY caused by a syntax error in the function, I think! :(
编辑:我认为这真的是由函数中的语法错误引起的!:(
function coolness() {
if[ [-z ""] -o [-z ""] ]; then
echo "Usage: function coolness() {
if [ -z "" -o -z "" ]; then
echo "Usage: function hello() {
echo "Hello, !"
}
export -f hello
[sub_package] [endpoint]";
exit 1;
fi
echo "Hi!"
}
[sub_package] [endpoint]";
exit 1;
fi
echo "Hi!"
}
采纳答案by Cascabel
The test in your function won't work - you should not have brackets around the -z clauses, and there should be a space between if and the open bracket. It should read:
您的函数中的测试将不起作用 - 您不应该在 -z 子句周围使用方括号,并且在 if 和左方括号之间应该有一个空格。它应该是:
function coolness() {
if [ -z "" -o -z "" ]; then
echo "Usage: if [[ -z "" || -z "" ]]; then
[sub_package] [endpoint]";
exit 1;
fi
echo "Hi!"
}
回答by Paused until further notice.
You can export functions. In your ~/.bashrc
file after you define the function, add export -f functionname
.
您可以导出函数。在~/.bashrc
定义函数后的文件中,添加export -f functionname
.
source .bashrc
Then the function will be available at the shell prompt and also in other scripts that you call from there.
然后该函数将在 shell 提示符下以及您从那里调用的其他脚本中可用。
Note that it's not necessary to export functions unless they are going to be used in child processes (the "also" in the previous sentence). Usually, even then, it's better to source
the function into the file in which it will be used.
请注意,没有必要导出函数,除非它们将在子进程中使用(上一句中的“也”)。通常,即使那样,最好source
将该函数放入将使用它的文件中。
Edit:
编辑:
Brackets in Bash conditional statements are not brackets, they're commands. They have to have spaces around them. If you want to group conditions, use parentheses. Here's your function:
Bash 条件语句中的括号不是括号,它们是命令。他们周围必须有空间。如果要对条件进行分组,请使用括号。这是你的功能:
[user@linuxPc]$ . ~/.bashrc
A better way to write that conditional is:
编写该条件的更好方法是:
$ source .bashrc
because the double brackets provide more capability than the single ones.
因为双括号比单括号提供了更多的功能。
回答by Lopoc
Include in your script the line
在您的脚本中包含该行
##代码##try with the sourceconstruct it should work!
尝试使用源结构它应该可以工作!
回答by mihai
Any changes made to .bashrc
will only take effect in a new terminal session. If you want to apply the changes to your current terminal session, you have to instruct the shell to re-read the .bashrc
. The shortest way to to this is to use the .
command, which is a synonym to source
:
对 所做的任何更改.bashrc
只会在新的终端会话中生效。如果要将更改应用于当前终端会话,则必须指示 shell 重新读取.bashrc
. 解决这个问题的最短方法是使用.
命令,它是 的同义词source
:
回答by PiedPiper
回答by Lerian Acosenossa
It's weird; my fuctin won't terminate untill I pass it to another bash instance, like this:
有点奇怪; 我的 fuctin 不会终止,直到我将它传递给另一个 bash 实例,如下所示:
bash myfunction
bash 我的函数