oh-my-bash icon indicating copy to clipboard operation
oh-my-bash copied to clipboard

Restarting Terminal After Installation Loses Oh My Bash Configuration

Open WarrenWongCodes opened this issue 3 years ago • 15 comments

After installing oh my bash in my bash terminal via the provided command:

bash -c "$(curl -fsSL https://raw.githubusercontent.com/ohmybash/oh-my-bash/master/tools/install.sh)"

Screen Shot 2022-02-03 at 11 19 42 PM

The theme shows up and I checked to ensure I have the .bashrc file configured as well as the .oh-my-bash file installed.

However, when I restart my terminal or create a new terminal tab, the configuration 'disappears'. Have tried multiple ways, no luck.

Details: macOS, Bash version 5.1.16(1)

Any ideas would be appreciated.

WarrenWongCodes avatar Feb 04 '22 04:02 WarrenWongCodes

I guess you forget to source ~/.bashrc from ~/.bash_profile.

Note: ~/.bashrc is used by non-login Bash and ~/.bash_profile is used by login Bash. We usually put the login-shell specific settings in ~/.bash_profile and the common settings in ~/.bashrc, and then source ~/.bashrc from ~/.bash_profile.

Edit: Or maybe I misunderstood the words "the configuration 'disappears'". What does it mean actually? I initially thought that's not possible, but do you possibly mean .bashrc and .oh-my-bash are deleted from the filesystem when you open a terminal?

akinomyoga avatar Feb 04 '22 04:02 akinomyoga

@akinomyoga Thank you for the response. It just visually disappears after I restart the terminal or create a new tab. The .bashrc and .oh-my-bash files are still there.

Also confirming that it is a login shell (bash). Do I or how do I source ~/.bashrc from ~./bash_profile ?

Not quite sure I follow what you mean to source ~/.bashrc from ~/.bash_profile , can you explain?

Current /bash_profile:

Screen Shot 2022-02-04 at 8 54 32 PM

Edit: also odd enough it seems like I can use oh-my-bash when running exec bash, but even when I set my shell to default /bin/bash it will not have the oh-my-bash configurations. (See screen shot)

Screen Shot 2022-02-04 at 9 09 45 PM

WarrenWongCodes avatar Feb 05 '22 02:02 WarrenWongCodes

Typical distribution includes the following three lines in .bash_profile by default (when there is .bash_profile). You can add these three lines at the end of your .bash_profile.

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

akinomyoga avatar Feb 05 '22 02:02 akinomyoga

That didn't work persay in terms of automatically using the .bashrc configuration upon terminal startup, but I found a solution that I should have tried in the first place....

terminal settings to run exec bash on startup: Screen Shot 2022-02-04 at 9 23 44 PM

^ this did the trick.

Although I don't know if this is the intended use of oh-my-bash to have to configure the terminal to run exec bash on startup.

WarrenWongCodes avatar Feb 05 '22 02:02 WarrenWongCodes

Edit: also odd enough it seems like I can use oh-my-bash when running exec bash,

That's because the shell newly started by exec bash is a non-login shell session, so it sources .bashrc.

but even when I set my shell to default /bin/bash it will not have the oh-my-bash configurations. (See screen shot)

I'm not sure what you mean by "it will not have the oh-my-bash configuration", but if you are talking about the black blank in the place of hostname, I now checked the theme and found that actually the hostnames are shown in bold black characters. In some terminals, these bold characters are shown in gray, but in some terminals including yours, these bold characters are shown by the true black color. I have now fixed the colors of characters in several themes in commit 8797ea3. Thank you for the report.

akinomyoga avatar Feb 05 '22 02:02 akinomyoga

That didn't work persay in terms of automatically using the .bashrc configuration upon terminal startup,

Hmm. that's strange, but I searched and found similar problems with macOS

Ah, I think possibly this comment is related to the reason why your .bash_profile is inactivated. What is the result of the following command?

$ ls -l -@ ~/.bash_profile

Although I don't know if this is the intended use of oh-my-bash to have to configure the terminal to run exec bash on startup.

Not really. By specifying exec bash, you can start a non-login shell session but may miss some necessary setups of the environment variables for a session, in theory. But I actually think that hardly causes real problems so is practically OK, but nevertheless, that's not the expected usage.

akinomyoga avatar Feb 05 '22 03:02 akinomyoga

Interesting however, my response from that command was:

-rw-r--r-- 1 username staff 55 Oct 11 12:53 /Users/username/.bash_profile

WarrenWongCodes avatar Feb 06 '22 20:02 WarrenWongCodes

OK, so the extended attributes are not the cause.

Then, what happens if you specify bash --login -i instead of exec bash in the terminal settings? If it works, that would be fine. You can continue to use that setting. This is kind of the normal setting.

If it doesn't work either, what happens if you specify exec -l bash --login -i? If this works, I think this should be fine too. It is not so normal, but there shouldn't be any difference in the result with the normal setting.

akinomyoga avatar Feb 06 '22 20:02 akinomyoga

I went through a similar issue. I have been using iTerm2 as a replacement for the macOSX default terminal. I had partially solved the issue by running exec bash at the startup of a shell in the past, just as @WarrenWongCodes did. I tried to run su my_username and it seemed to port my configuration as well. It always bothered me though, today I decided to fix it.

As @akinomyoga mentioned, .bashrc is for non-login shells, and .bash_profile for login shells. However, even after deleting my .bash_profile file, I was getting the same error. I realized that there must be another file being sourced. I found a .profile file overriding the other bash files. Try adding @akinomyoga recommendation to the .profile file instead.

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

This worked great for me! Now Oh My Bash configuration loads from both the Terminal and iTerm2

alexanderkoh avatar Jul 03 '22 00:07 alexanderkoh

I had the exact same problem that Oh My Bash was only working in non-login bash on my new MacBook Pro M1 Pro in the default terminal as well as iTerm2.

The reason for this issue turned out to be a non-existing .bash_profile file which needed the if statement pointed out above to apply Oh My Bash's configuration.

Can someone explain why Oh My Bash is not just creating a .bash_profile file if it does not exist? It shouldn't overwrite your files, off course, but I guess when a .bash_profile file exists, it already adds those lines. So why not do the same if the user hasn't created the file by himself yet? Because without this, Oh My Bash is not going to work flawlessly out of the box, or am I wrong?

Would be happy if this could be added or at least pointed out in the documentation, in case I'm not missing or misunderstanding anything. Thanks in advance! :)

jr-cologne avatar Feb 04 '23 15:02 jr-cologne

Thanks for the suggestion! I think that is a good idea.

but I guess when a .bash_profile file exists, it already adds those lines.

According to https://github.com/ohmybash/oh-my-bash/issues/302#issuecomment-1030496078, it doesn't seem to be always the case. I was thinking that the problem arises because the macOS creates ~/.bash_profile that doesn't contain source ~/.bashrc, but there is a chance if ~/.bash_profile isn't created in macOS by default. As you suggested, we can create it if it doesn't exist yet.

What do you think the contents of the default ~/.bash_profile in macOS should be? This is a tentative version:

# .bash_profile

# Load login settings and environment variables
if [[ -f ~/.profile ]]; then
  source ~/.profile
fi

# Load interactive settings
if [[ -f ~/.bashrc ]]; then
  source ~/.bashrc
fi

akinomyoga avatar Feb 04 '23 15:02 akinomyoga

Thank you very much for the quick response.

Unfortunately, it's actually my first time being on macOS so I am not the guy who could help you with trying to implement a good default bash profile. :laughing:

However, I can try whether your suggestion works on my end tomorrow. :)

jr-cologne avatar Feb 04 '23 16:02 jr-cologne

However, I can try whether your suggestion works on my end tomorrow. :)

Thanks!

This is also a suggested change for README (I haven't yet pushed it to the master).

akinomyoga avatar Feb 04 '23 16:02 akinomyoga

Hey @akinomyoga, I've tried your snippet above as well as installing Oh My Bash from your fork. The snippet works perfectly fine when pasting in into my bash profile. However, if I execute the following command after uninstalling Oh My Bash and clearing all my bash config files, I ran into an error:

bash -c "$(curl -fsSL https://raw.githubusercontent.com/akinomyoga/oh-my-bash/bash_profile/tools/install.sh)"
mv -f /Users/jasper/.oh-my-bash/templates/bash_profile.osh-template /Users/jasper/.bash_profile
mv: rename /Users/jasper/.oh-my-bash/templates/bash_profile.osh-template to /Users/jasper/.bash_profile: No such file or directory

Looks like there might be an issue in your install script.

The README change is pretty helpful, by the way. Thanks for including that. 👍

Edit: Sorry, I am stupid. Ignore what I just said above. Off course, it's not working because it's cloning the original repo, not yours, even if I run the install script from your fork.

I guess, everything should be fine. Thank you for your great work. 😄

jr-cologne avatar Feb 05 '23 10:02 jr-cologne

@jr-cologne Thank you for checking! I have pushed it to the master!

akinomyoga avatar Feb 05 '23 13:02 akinomyoga