19

When running this script, I run into an error on this line (relevant snippet below):

...
_NEW_PATH=$("$_THIS_DIR/conda" ..activate "$@")
if (( $? == 0 )); then
    export PATH=$_NEW_PATH
    # If the string contains / it's a path
    if [[ "$@" == */* ]]; then
        export CONDA_DEFAULT_ENV=$(get_abs_filename "$@")
    else
        export CONDA_DEFAULT_ENV="$@"
    fi

    # ==== The next line returns an error 
    # ==== with the message: "export: not valid in this context /Users/avazquez/anaconda3"
    export CONDA_ENV_PATH=$(get_dirname $_THIS_DIR)

    if (( $("$_THIS_DIR/conda" ..changeps1) ));  then
            CONDA_OLD_PS1="$PS1"
            PS1="($CONDA_DEFAULT_ENV)$PS1"
    fi
else
    return $?
fi
...

Why is that? I found this ticket, but I don't have that syntax error.

I found reports of the same problem in GitHub threads (e.g. here) and mailing lists (e.g. here)

3 Answers3

15

In zsh, Command Substitution result was performed word splitting if was not enclosed in double quotes. So if your command substitution result contain any whitespace, tab, or newline, the export command will be broken into parts:

$ export a=$(echo 1 -2)
export: not valid in this context: -2

You need to double quote command substitution to make it work, or using the safer syntax:

PATH=$_NEW_PATH; export PATH

or even:

PATH=$_NEW_PATH export PATH
cuonglm
  • 153,898
7

I think I got it, for POSIX compliance I need double quotes here. The following fixed it.

export CONDA_ENV_PATH="$(get_dirname "$_THIS_DIR")"

The following excellent article may be helpful:

0

This happened to me when I forgot to add the variable name to be exported as an alias in zshrc:

Wrong

export /Library/Java/JavaVirtualMachines/ibm-semeru-openjdk-17.jdk/Contents/Home

Good:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/ibm-semeru-openjdk-17.jdk/Contents/Home