回音输出到BASH中功能内的端子 [英] Echo output to terminal within function in BASH

查看:63
本文介绍了回音输出到BASH中功能内的端子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用BASH编写脚本.我在脚本中有一个功能,希望向用户提供进度反馈.唯一的问题是echo命令不会打印到终端.取而代之的是,将所有回声连接在一起并在最后返回.

I am writing a script in BASH. I have a function within the script that I want to provide progress feedback to the user. Only problem is that the echo command does not print to the terminal. Instead all echos are concatenated together and returned at the end.

考虑以下简化代码,我如何获得第一个回显以在用户终端中打印并将第二个回显作为返回值?

Considering the following simplified code how do I get the first echo to print in the users terminal and have the second echo as the return value?

function test_function {
    echo "Echo value to terminal"
    echo "return value"
}

return_val=$(test_function)

推荐答案

除了发送给STDERR之外,还没有其他解决方案(如果您的STDERR有其他用途,或者可能被调用方重定向,则可能是首选方案)

Yet a solution other than sending to STDERR (it may be preferred if your STDERR has other uses, or possibly be redirected by the caller)

此解决方案将打印直接输出到终端tty:

This solution direct prints to the terminal tty:

function test_function {
    echo "Echo value to terminal" > /dev/tty
    echo "return value"
}

这篇关于回音输出到BASH中功能内的端子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆