BASH Solution
In bash
there's a compgen
builtin that is able to "complete" a command (i.e. its trailing part). Its output is a LF-delimited list of the possible completions, and its return status is falsey when no match is found.
Here's an example of how you could use it to pick up the first available "python" command:
edit: I added the grep
filtering as mentioned in @StéphaneChazelas comment below.
#!/bin/bash if IFS= read -r python_exe then "$python_exe" -c 'print("Hello world!")' else echo 'python interpreter not found' >&2 exit 1 fi < <( compgen -c | grep -xE 'python([23](\.[0123456789]+)?)?' )
POSIX Solution
In a standard shell it's possible to write a function that mimics compgen -c
(at least for the commands that are in the PATH
) and then use it to get the "python" executable name:
#!/bin/sh compcom() ( case $1 in ( */* ) for f in "$1"*; do [ -f "$f" ] && [ -x "$f" ] && printf '%s\n' "$f"; done ;; ( * ) p=$PATH while [ -n "$p" ]; do d=${p%%:*} p=${p#"$d"} p=${p#:} [ -d "$d" ] || continue for f in "${d%/}/$1"*; do [ -f "$f" ] && [ -x "$f" ] && printf '%s\n' "${f##*/}"; done done ;; esac ) python_exe=$( compcom | grep -xE 'python([23](\.[0123456789]+)?)?' | head -n 1 ) [ -n "$python_exe" ] || { echo 'python interpreter not found' >&2 exit 1 } "$python_exe" -c 'print("Hello world!")'