5

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)

  • How about PATH=$_NEW_PATH; export PATH? – cuonglm Jun 10 '15 at 2:41
6

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
4

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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.