Sample Header Ad - 728x90

How to find the python command in a shell script?

1 vote
3 answers
144 views
Let's suppose that you have a super python 2&3 one-liner that you need to use in a shell script. On system A the python command works, but on system B you have to use python3, on system C you need to use python3.12 or python2, etc... What would be a sensible way to check some, if not every, "python*" command name? I want to avoid using a construct like this:
#!/bin/bash

if command -v python
then
    python_exe=python
elif command -v python3
then
    python_exe=python3
elif command -v python2
then
     python_exe=python2
else
    echo "python executable not found" >&2
    exit 1
fi > /dev/null

"$python_exe" -c 'print("Hello world!");'
Asked by Fravadona (1581 rep)
Mar 11, 2025, 08:13 PM
Last activity: Jul 16, 2025, 01:38 PM