bash 如何直接在 shell 函数内的标准输出上回显?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31656645/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:03:03  来源:igfitidea点击:

How do I echo directly on standard output inside a shell function?

bashshelloutput

提问by Aracthor

I, in my script, shell a function that prints a message on the console. It can be called from any other function.

我,在我的脚本中,shell 一个在控制台上打印消息的函数。它可以从任何其他函数调用。

function print_message
{
    echo "message content"
}

The problem is, in shell, functions like echoor printfthat usually print data on standard output redirect their messages to the calling function instead as a return value.

问题是,在 shell 中,像echoprintf通常在标准输出上打印数据的函数将它们的消息重定向到调用函数,而不是作为返回值。

return_value=$(print_message) # this line print nothing.
echo $return_value # This line print the message. I don't want to have to do it.

I would like to avoid this behavior and print it directly on standard - or error - output. Is there a way to do it?

我想避免这种行为并将其直接打印在标准或错误输出上。有没有办法做到这一点?

Or am I just wrong to want to use functions in shell, and should I use instead a huge script to handle any comportment?

或者我只是想在 shell 中使用函数是错误的,我应该使用一个巨大的脚本来处理任何行为吗?

回答by Etan Reisner

The $(...)calling syntax captures standard output. That is its job. That's what it does.

$(...)调用语法捕获标准输出。这就是它的工作。这就是它的作用。

If you want static messages that don't get caught by that then you can use standard error (though don't do this for things that aren't error message or debugging messages, etc. please).

如果您想要不被捕获的静态消息,那么您可以使用标准错误(尽管不要对不是错误消息或调试消息等的内容执行此操作)。

You can't have a function which outputs to standard output but that doesn't get caught by the $(...)context it is running in because there's only one standard output stream. The best you could do for that would be to detect when you have a controlling terminal/etc. and write directly to that instead (but I'd advise not doing that most of the time either).

您不能拥有一个输出到标准输出但不会被$(...)它运行的上下文捕获的函数,因为只有一个标准输出流。您可以做的最好的事情就是检测您何时拥有控制终端/等。并直接写信给它(但我建议大多数时候也不要这样做)。

To redirect to standard error for the function entirely you can do either of these.

要完全重定向到函数的标准错误,您可以执行以下任一操作。

print_message() {
    echo "message content" >&2
}

or

或者

print_message() {
    echo "message content"
} >&2

The difference is immaterial when there is only one line of output but if there are multiple lines of output then the latter is likely to be slightly more optimized (especially when the output stream happens to be a file).

当只有一行输出时,差异并不重要,但如果有多行输出,则后者可能会稍微优化一些(尤其是当输出流恰好是一个文件时)。

Also avoid the functionkeyword as it isn't POSIX/spec and isn't as broadly portable.

还要避免使用function关键字,因为它不是 POSIX/spec 并且不具有广泛的可移植性。

回答by that other guy

You are explicitly saying "don't print the output directly! Put it in a variable so I can print it myself!".

你明确地说“不要直接打印输出!把它放在一个变量中,这样我就可以自己打印了!”。

You can simply stop doing that, and the message will be printed automatically:

您可以简单地停止这样做,消息将自动打印:

$ cat yourscript 
#!/bin/bash

function print_message
{
    echo "message content"
}

print_message

$ ./yourscript
message content

回答by cacciatc

Invoking print_message inside $(...) redirects the output. If you don't want the output redirected then invoke the command without the $(...). E.g.

在 $(...) 中调用 print_message 会重定向输出。如果您不想重定向输出,则调用不带 $(...) 的命令。例如

 return_value=print_message # this line print nothing.
 echo $return_value # this line print the message. I don't want to have to do it.

Note, the return value from the function you provided will now be the name of the function.

请注意,您提供的函数的返回值现在将是函数的名称。