将Python版本插入到此处doc

时间:2015-03-31 06:44:29

标签: bash

#!/bin/bash
cat <<EOF
System python : `python -V`
EOF

显示如下

Python 2.6.6 System python :

我希望以这种方式展示

System python : Python 2.6.6

4 个答案:

答案 0 :(得分:2)

Python正在将版本信息发送到stderr而不是stdout。因此,当您运行python -v命令时,它会立即打印,而不是包含在命令的扩展中。将其stderr重定向到stdout以解决此问题:

cat <<EOF
System python : `python -V 2>&1`
EOF

答案 1 :(得分:2)

再使用反引号not recommended;更好地使用$()

VERSION=$(python -V 2>&1)
echo "System python : $VERSION"

(正如许多其他评论所述;我将stderr输出重定向到stdout;因为python -V打印到stderr)

答案 2 :(得分:1)

这将涵盖任何间距问题

alpha=$(python -V 2>&1)
echo "System python : $alpha"

Why $() is preferred

答案 3 :(得分:0)

这是我见过的最无用的猫!

使用echo:

echo 'System python :' `python -V 2>&1`

澄清:python -V将数据输出到stderr,因此它不会被shell捕获并立即打印。 2>&1将stderr重定向到stdout,所以一切正常。

相关问题