Software⏱️ 4 min read📅 2026-06-11

How to Fix: Function defined in my user's .bashrc causes an error when I sudo su

Error when sudo su causes bash: parse_git_branch: command not found. Solution involves modifying PS1 to use the correct user's shell.

Quick Answer: Modify PS1 to use the correct user's shell by adding a conditional statement that checks if the current user is the original user who defined the function.

The error occurs when you use `sudo su` and your user's `.bashrc` function, `parse_git_branch`, is carried over to another user's session. This can be frustrating because it affects the command prompt of other users who are logged in.

This issue can arise due to the way bash handles environment variables and functions when switching between users. The use of a Python virtualenv may also contribute to this problem, as it alters the environment in which bash is being used.

⚠️ Common Causes

  • The primary reason for this error is that the `PS1` variable is not reset properly when switching from one user's session to another. When you use `sudo su`, the new user inherits your current shell's settings, including the `PS1` variable, which includes your custom function `parse_git_branch`. This causes the function to be available in the new user's session.
  • An alternative reason for this issue is that the Python virtualenv may be interfering with bash's ability to handle environment variables and functions. When you source a virtualenv, it alters the environment in which bash is being used, potentially causing issues like this.

🚀 How to Resolve This Issue

Resetting the PS1 variable

  1. Step 1: To fix this issue, you can add the following line to your `.bashrc` file: `export PS1='\[$(tput setaf 7)\]\w \$USER@\$HOST:\$PATH'`. This sets a default `PS1` variable that does not include any functions, ensuring that when another user logs in, they will have their own default prompt.
  2. Step 2: Alternatively, you can use the `--login` option when using `sudo su`, like so: `sudo su --login`. This option tells bash to log in as a new shell for the target user, rather than inheriting your current shell's settings.
  3. Step 3: Another solution is to add the following line at the end of your `.bashrc` file: `if [ -z "$PS1" ]; then PS1='\[$(tput setaf 7)\]\w \$USER@\$HOST:\$PATH''; fi`. This checks if the `PS1` variable is empty, and if so, sets it to a default value. However, this method may not be as reliable, as it relies on the `PS1` variable being set before bash starts executing.

Modifying the virtualenv

  1. Step 1: To resolve the issue caused by the Python virtualenv, you can try sourcing the virtualenv after using `sudo su`. This ensures that the environment is altered only after the new user logs in.
  2. Step 2: Alternatively, you can add the following line to your `.bashrc` file: `source /path/to/virtualenv/bin/activate` before sourcing the virtualenv. This sets the environment variables for the virtualenv and allows bash to handle them properly.

🎯 Final Words

By resetting the `PS1` variable or modifying the Python virtualenv, you can resolve the issue of your user's function being carried over to another user's session when using `sudo su`. Remember to test these solutions thoroughly to ensure they work as expected.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions