Bash 函数 -> 找不到命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18611842/
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
Bash Function -> Command not found
提问by Andy
Hi gusy I am trying to learn Bash and cannot seem to get this basic script to work.
您好,我正在尝试学习 Bash,但似乎无法让这个基本脚本正常工作。
#!/bin/bash
function system_info
{
echo "function system_info"
}
$(system_info)
I get a function: command not found issue.
我得到一个函数:未找到命令的问题。
Any help much appreciated
非常感谢任何帮助
回答by Tim Cooper
Bash is trying to evaluate the string that is outputted by the system_info
function. You'll want to try the following, which will just simply run the function:
Bash 正在尝试评估system_info
函数输出的字符串。您将要尝试以下操作,它只会简单地运行该函数:
system_info
or to store the outputted value to a variable:
或将输出值存储到变量:
value=$(system_info)
回答by devnull
You need to invoke the function by saying:
您需要通过以下方式调用该函数:
system_info
$(...)
is used for command substitution.
$(...)
用于命令替换。
回答by sr01853
Invoke the function inside the script with just the function name and execute the script from the shell
仅使用函数名称调用脚本内的函数并从shell执行脚本
#!/bin/bash
function system_info {
echo "function system_info"
}
system_info
回答by Luis Tiago Flores Cristóv?o
#!/bin/bash
function system_info
{
echo "function system_info"
}
echo $(system_info)
Kind of redundant but it works without the command not found error.
有点多余,但它可以在没有命令未找到错误的情况下工作。
Or This:
或这个:
#!/bin/bash
function system_info
{
echo "function\n system_info"
}
printf "$(system_info)"
If you want to use newline character.
如果要使用换行符。
You can try this code in: https://www.tutorialspoint.com/execute_bash_online.php
您可以在以下位置尝试此代码:https: //www.tutorialspoint.com/execute_bash_online.php