无法将"python -V"的输出分配给bash中的变量

GJ.*_*GJ. 1 bash shell

我正在编写一个shell脚本,在检查系统上的python版本后做了一些工作.

我做了一个=`python -V`和$ a的回声:

[root@machine folder]# a=`python -V`
Python 2.3.4
[root@machine folder]# echo $a
Run Code Online (Sandbox Code Playgroud)

而且echo $a什么都不输出

同时我做:

[root@machine folder]# if grep "2.3.4" `python -V` ; then echo "bad" ; fi
Python 2.3.4
Run Code Online (Sandbox Code Playgroud)

点击后输入它只输出python的版本,但没有别的.

为什么会这样?还有其他方法可以让我做同样的任务吗?

Bri*_*new 5

python -V似乎输出stderr.如果你跑

a=`python -V` 
Run Code Online (Sandbox Code Playgroud)

输出显示在控制台上(即它没有被子shell分配拾取).但是,如果您重定向stderr到stdout,那么

a=`python -V 2>&1` 
Run Code Online (Sandbox Code Playgroud)

作品