Last active
December 7, 2023 17:57
-
-
Save Lotti/105922524341d0fcf97eadfafec7691c to your computer and use it in GitHub Desktop.
Bash script to set PHP executable in $PATH variable. To be used in Windows with MSYS2 MINGW64 bash shell. PHP releases must be already installed on a dedicated path.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
THIS_SCRIPT_PATH="/path/to/this/script" | |
PHP_VERSION="" | |
PHP_INSTALLATION_FOLDER="/path/to/php/versions" | |
PHP_FOLDER_PREFIX="php-" | |
PHP_PATH_PREFIX="${PHP_INSTALLATION_FOLDER}/${PHP_FOLDER_PREFIX}" | |
PHP_PATH="${PHP_PATH_PREFIX}${PHP_VERSION}" | |
if [[ -n "${PHP_VERSION}" ]]; then | |
export PATH="${PATH}:${PHP_PATH}" | |
fi | |
function php_list() { | |
ls "${PHP_INSTALLATION_FOLDER}" | cut -d ' ' -f1 | |
} | |
function php_set() { | |
if [ -d "${PHP_PATH_PREFIX}${1}" ]; then | |
sed -i -E 's#^export PHP_VERSION=.*#export PHP_VERSION="'"${1}"'"#g' "${THIS_SCRIPT_PATH}" | |
CLEANED_PATH=$(sed -E 's#:'"${PHP_PATH_PREFIX}"'.*[^:]?##g' <<<"${PATH}") | |
export PATH="${CLEANED_PATH}" | |
source "${THIS_SCRIPT_PATH}" | |
else | |
echo "No version found in path ${PHP_INSTALLATION_FOLDER}!" | |
fi | |
} | |
function php_get() { | |
if [[ -n "${PHP_VERSION}" ]]; then | |
echo "${PHP_VERSION}" | |
else | |
echo "No version set! Type php_list to get list of available releases!" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment