Skip to content
Niall Dooley edited this page Apr 24, 2024 · 9 revisions

Source: https://github.com/direnv/direnv/issues/408

Q: I've tried to read around how direnv works and I fail to understand why it doesn't override $PS1?

A: It's a natural question. However,

  1. $PS1 is usually a local variable and not an environment variable.
  2. There was a bug in macOS' bash 3.x implementation that would crash the shell if $PS1 was unset.

Those two combined led me to blacklist $PS1 as an environment variable that can be changed.

As a workaround, an option is to build a $PS1 that understands environment variables. In other words,

PS1='${CUSTOM_PS1:-default_PS1}: '

And then in the .envrc: export CUSTOM_PS1=mycustomPS1

For example, in the configuration file for an interactive shell (e.g. ~/.bashrc, ~/.zshrc etc.):

PS1='${VIRTUAL_ENV_PROMPT:+($VIRTUAL_ENV_PROMPT)}[\w]\n\$ '

And then in the .envrc within your project directory:

if [ -z "${VIRTUAL_ENV_PROMPT:-}" ] && [ -n "${VIRTUAL_ENV}" ]; then
    VIRTUAL_ENV_PROMPT=$(basename "${VIRTUAL_ENV}")
fi
export VIRTUAL_ENV_PROMPT

This creates the following prompt. Adjust to your taste.

(<basename of virtual environment if one exists, otherwise nothing>)[<full path of current working directory>]
$

Q: I'm not trying to override $PS1, but I'm getting this error anyway. How do I fix it?

A: This issue can occur when activating virtual environments or other scripts that try to change the prompt. To resolve it, add unset PS1 to the end of the .envrc and the error will stop appearing.

Clone this wiki locally