Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Get Python Version with Bash Script

Writer Matthew Martinez

I have a script that gets the python version from clients. The command in bash is:

pythonVer=$(ssh $user@$ip "python --version")

But this command shows the python version on stdout (my terminal that connects to clients) but I cannot assign this value to a variable. Just one server allows this, so when I tried to write this var to a file, I can see only one version in this file, other version numbers are in only my terminal.

What is this issue about the "python" command? How can I assign the client's python version to my var named pythonVer?

1 Answer

It seems python outputs its version to stderr, not stdout. So, redirect stderr to stdout in order to capture it

pythonVer=$(ssh "$user"@"$ip" 'python --version 2>&1')

or

pythonVer=$(ssh "$user"@"$ip" 'python --version' 2>&1)
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy