if-statementcmdconemu

ELSE was unexpected at this time


I frequently work on the Windows command line within ConEmu and occasionally, after working in a given window for some time, get the following unexpected error:

ELSE was unexpected at this time.

Currently, it occurred after pasting about 150 lines of rm commands (may be unrelated).

Once I get that error, I cannot use the ELSE statement in that window; I must start a new shell process, at which point my scripts work as expected. For example, once that error occurs in a window, I observe the following:

C:\> IF DEFINED AN_ENV_VAR (ECHO YES) ELSE ECHO NO
ELSE was unexpected at this time.
C:\> IF NOT DEFINED AN_ENV_VAR (ECHO YES) ELSE ECHO NO
ELSE was unexpected at this time.
C:\> IF DEFINED AN_ENV_VAR (ECHO YES)
C:\> IF NOT DEFINED AN_ENV_VAR (ECHO YES)
YES

A new shell gives expected results:

C:\> IF DEFINED AN_ENV_VAR (ECHO YES) ELSE ECHO NO
NO
C:\> IF NOT DEFINED AN_ENV_VAR (ECHO YES) ELSE ECHO NO
YES
C:\> IF DEFINED AN_ENV_VAR (ECHO YES)
C:\> IF NOT DEFINED AN_ENV_VAR (ECHO YES)
YES

Is there any way to fix the current shell?


Solution

  • One of the environment variables should contain a miss adding else statement.

    Please check the expanded values of the DEFINED AN_ENV_VAR.

    One way to reproduce the error is

    set var=123 else echo abc
    if %var%==123 echo ok
    

    When the var expanded. There will be a additional else statement. Then the error is raised.

    ELSE was unexpected at this time.