How to append output of “python --version” to a fi

2019-02-16 01:52发布

I'm trying to write a short script to log certain environment variables of my current shell session to file. Unfortunately the output of "python --version" seems to ignore (?) the >> operator and prints to the shell instead to the file.

My minimal (not) working example:

rm path.log
echo "python --version" >> path.log
python --version >> path.log

I would expect that the file path.log would then have the following content:

python --version
Python 2.6.6

But the line "Python 2.6.6" is printed to the shell and not to the file. How can I fix this?

Thank you!

PS: This works completely fine for

gcc --version

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-16 02:39

python --version outputs to STDERR.

You need to merge STDERR into STDOUT:

python --version >> path.log 2>&1

For reference, you can verify such behavior by saying:

$ python --version 1>/dev/null
Python 2.7.4

The STDOUT in the above example was redirected to /dev/null. This would imply that the output is being sent to STDERR.

查看更多
迷人小祖宗
3楼-- · 2019-02-16 02:39

a simpler solution is:

python --version 2>> path.log
查看更多
登录 后发表回答