I am using zsh as my shell application. While activating the environment, I encountered this error:
voyager-sdk git:(release/v1.3) ✗ source venv/bin/activate _AX_set_env:4: bad substitution
Do you think providing support for shells other than bash would help?
Best answer by emustafa
Hi @mcunha
Here is a super unofficial, hacky and totally not tested solution:
Open venv/bin/activate in an editor and adapt the following two functions.
_AX_set_env() { # Must be called before defining deactivate function local var="_OLD_ENV_${1}" local x= # Check if deactivate function exists (bash/zsh compatible) local deactivate_exists=0 if [ -n "${BASH:-}" ]; then type deactivate &>/dev/null && deactivate_exists=1 else typeset -f deactivate &>/dev/null && deactivate_exists=1 fi # Indirect variable expansion for bash and zsh if [ -n "${BASH:-}" ]; then if [ -n "${!1:-}" ] && [ $deactivate_exists -eq 0 ] ; then eval "${var}=\"${!1}\"" fi else # zsh indirect expansion if [ -n "${(P)1:-}" ] && [ $deactivate_exists -eq 0 ] ; then eval "${var}=\"${(P)1}\"" fi fi # strip escapes from $ expressions and evaluate RHS before assigning to LHS x=${2//\\\$/\$} x=`echo $x` eval "$1=\"$x\"" export "$1" }
for _AX_var in "${_AX_envs[@]}"; do if [[ "$_AX_var" == *=* ]]; then _AX_set_env "${_AX_var%%=*}" "${_AX_var#*=}" fi done unset _AX_var
_AX_reset_env() { local var="_OLD_ENV_${1}" if [ -n "${BASH:-}" ]; then if [ -n "${!var:-}" ]; then eval "$1=\"${!var}\"" export "$1" unset ${var} else unset $1 fi else # zsh indirect expansion if [ -n "${(P)var:-}" ]; then eval "$1=\"${(P)var}\"" export "$1" unset ${var} else unset $1 fi fi }
Yeah, that looks like bash-specific syntact that zsh doesn’t support (by default anyway, as far as I know).
I actually think this is the first time I’ve seen it raised, but it’s a really interesting point - support for more terminal options. It’s probably not going to be a huge priority right now, if I’m honest, but I like the idea!
Actually, it might be worth adding it as an idea/feature request in the Launchpad. If it gets plenty of attention, it might gain more traction internally. 👍
Here is a super unofficial, hacky and totally not tested solution:
Open venv/bin/activate in an editor and adapt the following two functions.
_AX_set_env() { # Must be called before defining deactivate function local var="_OLD_ENV_${1}" local x= # Check if deactivate function exists (bash/zsh compatible) local deactivate_exists=0 if [ -n "${BASH:-}" ]; then type deactivate &>/dev/null && deactivate_exists=1 else typeset -f deactivate &>/dev/null && deactivate_exists=1 fi # Indirect variable expansion for bash and zsh if [ -n "${BASH:-}" ]; then if [ -n "${!1:-}" ] && [ $deactivate_exists -eq 0 ] ; then eval "${var}=\"${!1}\"" fi else # zsh indirect expansion if [ -n "${(P)1:-}" ] && [ $deactivate_exists -eq 0 ] ; then eval "${var}=\"${(P)1}\"" fi fi # strip escapes from $ expressions and evaluate RHS before assigning to LHS x=${2//\\\$/\$} x=`echo $x` eval "$1=\"$x\"" export "$1" }
for _AX_var in "${_AX_envs[@]}"; do if [[ "$_AX_var" == *=* ]]; then _AX_set_env "${_AX_var%%=*}" "${_AX_var#*=}" fi done unset _AX_var
_AX_reset_env() { local var="_OLD_ENV_${1}" if [ -n "${BASH:-}" ]; then if [ -n "${!var:-}" ]; then eval "$1=\"${!var}\"" export "$1" unset ${var} else unset $1 fi else # zsh indirect expansion if [ -n "${(P)var:-}" ]; then eval "$1=\"${(P)var}\"" export "$1" unset ${var} else unset $1 fi fi }
Here is a super unofficial, hacky and totally not tested solution:
Open venv/bin/activate in an editor and adapt the following two functions.
_AX_set_env() { # Must be called before defining deactivate function local var="_OLD_ENV_${1}" local x= # Check if deactivate function exists (bash/zsh compatible) local deactivate_exists=0 if [ -n "${BASH:-}" ]; then type deactivate &>/dev/null && deactivate_exists=1 else typeset -f deactivate &>/dev/null && deactivate_exists=1 fi # Indirect variable expansion for bash and zsh if [ -n "${BASH:-}" ]; then if [ -n "${!1:-}" ] && [ $deactivate_exists -eq 0 ] ; then eval "${var}=\"${!1}\"" fi else # zsh indirect expansion if [ -n "${(P)1:-}" ] && [ $deactivate_exists -eq 0 ] ; then eval "${var}=\"${(P)1}\"" fi fi # strip escapes from $ expressions and evaluate RHS before assigning to LHS x=${2//\\\$/\$} x=`echo $x` eval "$1=\"$x\"" export "$1" }
for _AX_var in "${_AX_envs[@]}"; do if [[ "$_AX_var" == *=* ]]; then _AX_set_env "${_AX_var%%=*}" "${_AX_var#*=}" fi done unset _AX_var
_AX_reset_env() { local var="_OLD_ENV_${1}" if [ -n "${BASH:-}" ]; then if [ -n "${!var:-}" ]; then eval "$1=\"${!var}\"" export "$1" unset ${var} else unset $1 fi else # zsh indirect expansion if [ -n "${(P)var:-}" ]; then eval "$1=\"${(P)var}\"" export "$1" unset ${var} else unset $1 fi fi }
Wow, nice work dude! 😃 I think that deserves a System Sensei badge 😎