How to append output of “python --version” to a file in bash shell?

不羁岁月 提交于 2019-12-19 04:50:10

问题


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

回答1:


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.




回答2:


a simpler solution is:

python --version 2>> path.log


来源:https://stackoverflow.com/questions/22171715/how-to-append-output-of-python-version-to-a-file-in-bash-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!