bashanacondaconda

Syntax error near unexpected token 'elif' in bash concerning conda


I have a problem with my bash file: each time I launch the terminal, this error occurs:

bash: /home/hk/.bashrc: line 125: syntax error near unexpected token elif' bash: /home/hk/.bashrc: line 125: elif [ -f "$HOME/miniconda3/etc/profile.d/conda.sh" ]; then'

This started to happen when I installed Anaconda.

This is what I get when I run it in shellcheck:

$ shellcheck myscript
 
Line 123:
  if [ -n "$CONDA_EXE" ] && [ -f "$(dirname "$CONDA_EXE")/../etc/profile.d/conda.sh" ]; then
  ^-- SC1009 (info): The mentioned syntax error was in this if expression.
>>                                                                                      ^-- SC1073 (error): Couldn't parse this then clause. Fix to allow more checks.
 
Line 125:
  elif [ -f "$HOME/miniconda3/etc/profile.d/conda.sh" ]; then
  ^-- SC1048 (error): Can't have empty then clauses (use 'true' as a no-op).
       ^-- SC1072 (error): Unexpected keyword/token. Fix any mentioned problems and try again.

This is the problomatic code:

conda_lazy() {
  unset -f conda
  if [ -n "$CONDA_EXE" ] && [ -f "$(dirname "$CONDA_EXE")/../etc/profile.d/conda.sh" ]; then
# . "$(dirname "$CONDA_EXE")/../etc/profile.d/conda.sh"  # commented out by conda initialize
  elif [ -f "$HOME/miniconda3/etc/profile.d/conda.sh" ]; then
# . "$HOME/miniconda3/etc/profile.d/conda.sh"  # commented out by conda initialize
....rest of the code }

Fixed, this is the whole conda_lazy() fuction :

conda_lazy() {
  unset -f conda
  if [ -n "$CONDA_EXE" ] && [ -f "$(dirname "$CONDA_EXE")/../etc/profile.d/conda.sh" ]; then
    true # . "$(dirname "$CONDA_EXE")/../etc/profile.d/conda.sh"  # commented out by conda initialize
  elif [ -f "$HOME/miniconda3/etc/profile.d/conda.sh" ]; then
    true # . "$HOME/miniconda3/etc/profile.d/conda.sh"  # commented out by conda initialize
  elif [ -f "$HOME/anaconda3/etc/profile.d/conda.sh" ]; then
    true # . "$HOME/anaconda3/etc/profile.d/conda.sh"  # commented out by conda initialize
  elif [ -f "/opt/conda/etc/profile.d/conda.sh" ]; then
    true # . "/opt/conda/etc/profile.d/conda.sh"  # commented out by conda initialize
  fi
  if ! command -v conda >/dev/null 2>&1; then
    true # fallback: try adding likely bin dirs to PATH
    PATH="$HOME/miniconda3/bin:$HOME/anaconda3/bin:/opt/conda/bin:$PATH"
  fi
  conda "$@"
}

Solution

  • Quoting your own output back to you:

      elif [ -f "$HOME/miniconda3/etc/profile.d/conda.sh" ]; then
      ^-- SC1048 (error): Can't have empty then clauses (use 'true' as a no-op).
    

    Shellcheck is telling you how to fix the problem. Read what it's saying and follow the instructions.

    In this case, that means putting some kind of placeholder so you don't have completely empty blocks in your if statements. true or its synonym : are traditional choices of commands to use in this role.

    conda_lazy() {
      unset -f conda
      if [ -n "$CONDA_EXE" ] && [ -f "$(dirname "$CONDA_EXE")/../etc/profile.d/conda.sh" ]; then
        true # . "$(dirname "$CONDA_EXE")/../etc/profile.d/conda.sh"  # commented out by conda initialize
      elif [ -f "$HOME/miniconda3/etc/profile.d/conda.sh" ]; then
        true # . "$HOME/miniconda3/etc/profile.d/conda.sh"  # commented out by conda initialize