将命令输出重定向到 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)