将命令输出重定向到 bash 中的变量失败 [英] Redirecting command output to a variable in bash fails
本文介绍了将命令输出重定向到 bash 中的变量失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将命令输出重定向到一个变量:
I'm trying to redirect command output to a variable:
OUTPUT=$(sudo apache2ctl configtest)
然后阅读它:
echo $OUTPUT
运行时输出如下:
19:19:12 user@user ~ OUTPUT=$(sudo apache2ctl configtest)
Syntax OK
Syntax OK
但变量保持空白.我已经对其他命令尝试了相同的方法,一切正常.
But the variable stays blank. I've tried the same for other commands and everything works fine.
OUTPUT=$(ls -l)
这会将文件列表写入变量 OUTPUT
以便稍后读取.我该怎么做才能让它发挥作用?
This writes file list to variable OUTPUT
so that it can be read later.
What should i do to make it work?
推荐答案
也许输出到 stderr
而不是 stdout
?试试这个:
Maybe the output goes to stderr
, not stdout
? Try this:
OUTPUT=$(sudo apache2ctl configtest 2>&1)
这篇关于将命令输出重定向到 bash 中的变量失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文