Zsh: export: not valid in this context

46,177

Solution 1

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

Solution 2

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:

Share:
46,177

Related videos on Youtube

Amelio Vazquez-Reina
Author by

Amelio Vazquez-Reina

I'm passionate about people, technology and research. Some of my favorite quotes: "Far better an approximate answer to the right question than an exact answer to the wrong question" -- J. Tukey, 1962. "Your title makes you a manager, your people make you a leader" -- Donna Dubinsky, quoted in "Trillion Dollar Coach", 2019.

Updated on September 18, 2022

Comments

  • Amelio Vazquez-Reina
    Amelio Vazquez-Reina almost 2 years

    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)