Skip to content

[Bash] activate script incorrectly uses $0 instead of BASH_SOURCE for relocation fallback #3090

@sbagri

Description

@sbagri

Issue

When a virtual environment (created using virtualenv.pyz) is activated from a directory other than its own root (using either an absolute or relative path), the PATH variable is not updated correctly. Instead of pointing to the virtual environment's bin directory, it incorrectly adds a path relative to the user's current location or the shell's location. I have verified this locally on my system

This occurs because the activate script's fallback logic for determining VIRTUAL_ENV relies on realpath "${0}". Since the script is intended to be sourced in bash, ${0} evaluates to the name of the shell (e.g., /bin/bash), causing the script to calculate an incorrect root directory (often resulting in <current directory>/bin being added to the PATH).

Environment

  • OS: Debian GNU/Linux rodete
  • Kernel: Linux 6.17.13-1rodete1-amd64
  • Architecture: x86-64
  • Shell: bash
  • Python version and path: Python 3.13.12 , /usr/bin/python3
  • pip list of the host python where virtualenv is installed:
    virtualenv 20.36.1
    Others are not relevant here.
  • The details of virtualenv.pyz: [virtualenv.pyz](https://bootstrap.pypa.io/virtualenv.pyz) 27-Feb-2026 09:48 8378751

The exact problematic code
5d09a90

Potentially fixed code
In the above code, the CURRENT_PATH=$(realpath "${0}") should be replaced with CURRENT_PATH=$(realpath "${BASH_SOURCE[0]}")

If the community agrees that this is the right fix then I can create a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions