ohmybash / oh-my-bash

A delightful community-driven framework for managing your bash configuration, and an auto-update tool so that makes it easy to keep up with the latest updates from the community.
https://ohmybash.github.io
MIT License
5.53k stars 624 forks source link

.bashrc is running twice when SSH into remote server #537

Closed cmonty14 closed 3 months ago

cmonty14 commented 4 months ago

Hi, I deployed ohmybash successfully. I customized .bashrc in order to start tmux session automatically when SSH into a remote server.

However, .bashrc is executed twice. I added this code to .bashrc

if [[ $(hostname) != "vlgsplts02" ]] && [[ $(hostname) != "dlm-lts01" ]]; then
  echo "Hello $USER"
fi

and now I get this in CLI: image

Can you please advise how to fix it?

THX

akinomyoga commented 3 months ago

Sorry for the slow reply. I was busy last week, and I don't have an idea what could cause the behavior in your environment. I need to collect information. What would be the result if you replace echo "Hello $USER" in the above test code as follows?

 if [[ $(hostname) != "vlgsplts02" ]] && [[ $(hostname) != "dlm-lts01" ]]; then
-  echo "Hello $USER"
+  declare -p BASHPID BASH_SOURCE BASH_LINENO >&2
 fi
cmonty14 commented 3 months ago

Thanks for your reply and your support in analyzing this issue.

After modifying .bashrc I get this output:

declare -ir BASHPID="20144"
declare -a BASH_SOURCE=([0]="/sapmnt/HOME/d038783/.bashrc" [1]="/etc/profile")
declare -a BASH_LINENO=([0]="349" [1]="0")
declare -ir BASHPID="20144"
declare -a BASH_SOURCE=([0]="/sapmnt/HOME/d038783/.bashrc" [1]="/sapmnt/HOME/d038783/.bash_profile")
declare -a BASH_LINENO=([0]="5" [1]="0")

Can you conclude from this output why .bashrc is running twice?

akinomyoga commented 3 months ago

Thank you for the information. So ~/.bashrc is sourced from two different places. One place is /etc/profile. The other place is ~/.bash_profile. OMB creates ~/.bash_profile if it doesn't exist because missing ~/.bash_profile causes problems in macOS.

What is your distribution? I'd say /etc/profile sourcing ~/.bashrc is strange. As described in Bash Reference Manual [1] and other places [2,3], ~/.bashrc is supposed to be sourced from ~/.bash_profile. So /etc/profile is not supposed to source ~/.bashrc.

I searched. For example, openSUSE /etc/profile seems to source ~/.bashrc. I have to say the default Bash configurations of openSUSE have been causing problems constantly. It's not maintained well. Maybe the ideal solution is to fix the setup in the distribution, but I'd anticipate that it would be difficult because there are existing users assuming the strange setup of the distributions.

This time, I think you can just comment out the "source ~/.bashrc" part of your ~/.bash_profile.

cmonty14 commented 3 months ago

Actually I'm running SLES, but this should be equal to openSUSE. However I was using a customized ~/.bashrc before deploying oh-my-bash, and this error was not showing up.

I will now modify ~/.bash_profile and verify if this is working.

Update: Removing source ~/.bashrc from ~/.bashrc is the solution.