How to Use the conda Command from Shell Scripts

How to Use the conda Command from Shell Scripts

Hello!

Today's topic is the conda command, a tool many of us rely on every day.
Here are some tips for shell scripts and batch tasks that enter a conda virtual environment, run some processing, and then return.

In AI development, Anaconda and its core conda package manager are extremely useful.
However, when you try to use conda automatically from a shell script, you run into an unexpected hurdle.

In this article, we explain how to correctly invoke the conda command from a shell script.

conda and the Non-Interactive Mode Problem

In a Linux environment where Anaconda is installed, the conda command is normally initialized by configuration files such as .bashrc or .bash_profile.

When you use a shell casually, it is easy to forget about this conda initialization, but in most cases these settings are designed to take effect only in the shell's interactive mode.

As a result, in non-interactive contexts such as shell scripts, the conda command does not work properly.

For example, the .bashrc file's conda initialization section contains a condition like this:

# >>> conda initialize >>>
if [[ $- == *i* ]]; then  # Run only in interactive mode
    . "/path/to/anaconda3/etc/profile.d/conda.sh"
fi
# <<< conda initialize <<<

Here, if [[ $- == *i* ]] is the interactive-mode check. In a non-interactive environment such as a shell script, this condition is not met, so conda initialization never runs.
In other words, the conda command does not work properly inside a shell script.

The Solution: An enable_conda.sh Script

To solve this problem, let's create a script like the following:

#!/bin/bash
###[enable_conda.sh]###########################################################
# Explicitly initialize conda
# Bypass the interactive-mode check in ~/.bashrc by running the initialization directly

# Use the user's home directory
CONDA_PATH="$HOME/anaconda3"

# Run the conda initialization block directly
__conda_setup="$('$CONDA_PATH/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "$CONDA_PATH/etc/profile.d/conda.sh" ]; then
        . "$CONDA_PATH/etc/profile.d/conda.sh"
    else
        export PATH="$CONDA_PATH/bin:$PATH"
    fi
fi
unset __conda_setup

# Verify the path
echo "conda path in use: $(which conda)"
echo "conda version: $(conda --version)"
###[/enable_conda.sh]#########################################################

This script:

  1. Bypasses the interactive-mode check and runs the conda initialization code directly
  2. Sets the environment variables appropriately so the conda command can also be used from within shell scripts

The Importance of the source Command

Now, the key to making this script effective is to run it with the source command:

source ./enable_conda.sh
# or
. ./enable_conda.sh  # dot command (equivalent to source)

Refresher: What Is the source Command?

source command is a shell builtin that runs the commands in a specified file directly within the current shell process. It has the following characteristics:

  • The commands in the file are executed within the current shell process
  • Changes to the shell's state—such as modified environment variables and defined aliases—are reflected in the current session
  • . (dot) command provides exactly the same functionality

The Difference Between source and Direct Execution

We rarely think about it in day-to-day work, but here is how running a script with source differs from executing it directly:

Execution method Process Effect on environment variables For conda
sh script.sh Creates a new shell process (child process) Environment variables set in the script are lost when it exits Initialization takes effect only in the child process; the conda command is unavailable in the parent shell
source script.sh Runs directly within the current shell process Environment variable changes are kept in the current shell conda is initialized in the current shell, and the conda command can be used from then on

For scripts that initialize environment-management tools such as conda, the changes must be reflected in the current shell environment, so always use the source command.

Practical Examples

You can use the script as follows.

Batch processing script

#!/bin/bash
# Data processing batch job

# Initialize conda
source /path/to/enable_conda.sh

# Activate a specific environment
conda activate myenv

# Run the Python script
python /path/to/process_data.py

# Return to the base environment when processing is complete
conda deactivate

Scheduled (cron) job

crontab file:

# Run the data update every day at 2:00 AM
0 2 * * * /bin/bash /path/to/daily_update.sh

daily_update.sh:

#!/bin/bash
# Initialize conda
source /home/user/scripts/enable_conda.sh

# Activate the environment
conda activate analysis_env

# Run the script
python /home/user/projects/update_database.py

# Write a log entry
echo "$(date): database update complete" >> /home/user/logs/cron.log

As you can see, the initialization code is not that long, so instead of keeping it in a separate file, it is also perfectly fine to put the conda initialization code directly into the script you want to run.

Summary

To use the conda command from a shell script:

  1. Create an initialization script (enable_conda.sh) that bypasses the interactive-mode check (or embed the code directly in your script)
  2. If you keep it as a separate script, run it with the source command so that the current shell environment picks up the conda settings
  3. Use environment variables so the script can be reused across different environments

With that, you can now use conda freely inside your shell scripts.

Read more