From 6971f8ae32c7baf6aedae3de6acee8996040e847 Mon Sep 17 00:00:00 2001 From: Phil Carns Date: Wed, 13 Feb 2019 20:52:18 -0500 Subject: make SPACK_SHELL detection more robust (#9712) Spack shell detection in setup-env.sh was originally based on examining the executable name of $$ (from "ps"). In some cases this does not actually give the name of the shell used, for example when setup-env.sh was invoked from a script using "#!". To make shell detection more robust, this adds a preliminary check for shell variables which indicate that the shell is bash or zsh; the executable name of $$ is retained as a fallback if those variables are not defined. --- share/spack/setup-env.sh | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'share') diff --git a/share/spack/setup-env.sh b/share/spack/setup-env.sh index a8e918c1c9..f82a55d8f1 100755 --- a/share/spack/setup-env.sh +++ b/share/spack/setup-env.sh @@ -236,7 +236,18 @@ export SPACK_ROOT=${_sp_prefix} # Determine which shell is being used # function _spack_determine_shell() { - PS_FORMAT= ps -p $$ | tail -n 1 | awk '{print $4}' | sed 's/^-//' | xargs basename + # This logic is derived from the cea-hpc/modules profile.sh example at + # https://github.com/cea-hpc/modules/blob/master/init/profile.sh.in + # + # The objective is to correctly detect the shell type even when setup-env + # is sourced within a script itself rather than a login terminal. + if [ -n "${BASH:-}" ]; then + echo ${BASH##*/} + elif [ -n "${ZSH_NAME:-}" ]; then + echo $ZSH_NAME + else + PS_FORMAT= ps -p $$ | tail -n 1 | awk '{print $4}' | sed 's/^-//' | xargs basename + fi } export SPACK_SHELL=$(_spack_determine_shell) -- cgit v1.2.3-70-g09d2