Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 从“中读取特定信息”;“回声”;在打印时忽略其他命令_Bash_Shell_Sh - Fatal编程技术网

Bash 从“中读取特定信息”;“回声”;在打印时忽略其他命令

Bash 从“中读取特定信息”;“回声”;在打印时忽略其他命令,bash,shell,sh,Bash,Shell,Sh,我正在编写一些bash脚本,我面临以下问题。比如说: function echo_ignore_print() { echo "ignoring this print" # print this line for the user without storing its value value=187fef echo "$value" # return this value } info_to_keep=$(echo_ignore_print) echo "$info

我正在编写一些bash脚本,我面临以下问题。比如说:

function echo_ignore_print()
{
    echo "ignoring this print" # print this line for the user without storing its value
    value=187fef
    echo "$value" # return this value
}

info_to_keep=$(echo_ignore_print)

echo "$info_to_keep"   #   ignoring this print 187fef
我需要能够在脚本到达该行时打印“忽略此打印”,但我还需要返回变量“$value”的值,以便以后使用它。我宁愿不要全局变量

因此,有没有办法转储echo命令的所有字符串并保存一些以备将来使用?或者有其他的方法吗


谢谢您的帮助。

您可以在
stderr
上编写被忽略的内容,而不是
stdout

echo_ignore_print() {
   echo "ignoring this print" >&2
   value='187fef'
   echo "$value"
}

info_to_keep=$(echo_ignore_print)
ignoring this print

echo "$info_to_keep"
187fef

正确的做法是使用
echo“忽略此打印”>&2
,以便该行指向不同的文件描述符(在本例中为标准错误)。(作为注释发布,因为我不知道是否可以修改函数本身。)