Anaconda安装及执行source ~/.bashrc时出现command not found: shopt错误
Hey there, let's break down why you're hitting that shopt error and how to fix it quickly:
What's causing this?
The shopt command is a bash-only built-in tool—it doesn't exist in other shells like zsh, fish, etc. When you run source ~/.bashrc in a non-bash shell, any bash-specific commands (like shopt) will throw a "command not found" error. Anaconda probably added some bash-specific config lines to your ~/.bashrc during installation, which triggers this when you're using a different default shell.
Step 1: Confirm your current shell
First, check which shell you're actually using:
echo $SHELL
If the output is something like /bin/zsh instead of /bin/bash, that's the root cause.
Step 2: Choose a fix that works for you
Pick one of these solutions based on your preference:
Switch to bash temporarily or permanently
- To use bash just for this session: run
bash, then executesource ~/.bashrc—the error should disappear, and Anaconda's environment will load correctly. - To set bash as your default shell (requires restarting your terminal):
chsh -s /bin/bash
- To use bash just for this session: run
Move Anaconda's config to your shell's config file
If you prefer sticking with your current shell (like zsh), open~/.bashrcand copy the entire Anaconda initialization block—it looks like this:>>> conda initialize >>>!! Contents within this block are managed by 'conda init' !!__conda_setup="$('/path/to/conda/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/path/to/conda/etc/profile.d/conda.sh" ]; then
. "/path/to/conda/etc/profile.d/conda.sh"
else
export PATH="/path/to/conda/bin:$PATH"
fi
fi
unset __conda_setup<<< conda initialize <<<Paste this block into your shell's config file (e.g.,
~/.zshrcfor zsh), then runsource ~/.zshrc—Anaconda will load without hitting theshopterror.Guard the bash-specific code in ~/.bashrc
If you want to keep~/.bashrccompatible with other shells, wrap theshoptline (and any other bash-only commands) in a conditional that only runs in bash:if [ -n "$BASH_VERSION" ]; then # Your shopt command here, e.g.: shopt -s expand_aliases fiNow when you run
source ~/.bashrcin a non-bash shell, this code will skip over theshoptcommand, avoiding the error.
内容的提问来源于stack exchange,提问作者Mohamed Baher




