Resolve Magento 1 / Magento 2 root folder running inside the Magento tree but w/o Magento env, shell (bash) module

PHOTO EMBED

Sat Jan 08 2022 06:07:58 GMT+0000 (Coordinated Universal Time)

Saved by @juliyvchirkov #bash #magento #document_root #resolve #shell

#!/usr/bin/env bash
#
# Returns Magento document root if sourced or prints if launched as standalone
#

mageRoot="$(dirname $(realpath "${0}"))"

while [[ ! -e ${mageRoot}/app/Mage.php && ! -e ${mageRoot}/bin/magento ]]
do
    mageRoot="$(dirname $(realpath ${mageRoot}))"
done

return "${mageRoot}" 2>/dev/null || printf '%s' "${mageRoot}" && exit
content_copyCOPY

https://gist.github.com/juliyvchirkov/2c5c8d54b182528e39d4565d1632f8ee#file-magentodocumentroot-sh