Why does activating conda unload my .bashrc configurations? - bash

Ever since I added conda activate to my .bashrc file, opening up a new terminal on MacOS does seem to "unload" my .bashrc configuration. I have to manually call source ~/.bashrc every time.
Here is the content of my .bashrc file
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
[ -f /usr/local/etc/profile.d/autojump.sh ] && . /usr/local/etc/profile.d/autojump.sh
. /usr/local/anaconda3/etc/profile.d/conda.sh
conda activate base
export GPG_TTY=$(tty)
If I want to use the j command now, I first have to source the .bashrc file again.
Anybody know what is causing this and how to fix it?

So after some digging, I found out that activating conda starts bash with .bash_profile, so I copied my .bashrc contents into .bash_profile and now everything works smoothly again.

Related

Why do my $PATH environment always reset after I open a new terminal on my mac?

Usually I will nano .zsh_profile
Then I will edit the path
#PYTHON
export PATH=/Users/ffff/Library/Python/3.8/bin:$PATH
# JAVA
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
#ANDROID
export ANDROID_HOME=/Users/ffff/Library/Android/sdk
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/tools/bin:$PATH
then I will save and exit
Then I will
source .zsh_profile
I will test the Java and ADB all is good, but one I open a new Terminal from my mac, it will say ADB and JAVA and Android_HOME not found
Why is it not persist? Did I miss out anything? My mac version is 12.2.1
The .zsh_profile file, has no special meaning. This is probably a confusion form Bash, where the bash_profile is executes for login shells. In Zsh, that equivalent would be ~/.zprofile. You might have some code in your .zshrc file, that overrides the PATH variable with something else. Because, as oppsed to .zprofile, the .zshrc file gets executed for every interactive non-login shells.
So my advice, checkout .zshrc and see if there's something overriding the PATH there, if so, maybe you want to change that, and NOT to execute these commands you want to add for every time a shell is opened, you should put them in .zprofile, so they only get executed once at login.

macOS Catalina 10.15(beta) - Why is ~/.bash_profile not sourced by my shell?

I want to set the environment variable I added below the line to ~/.bash_profile and ~/.profile but it didn't work.
export JBOSS_HOME=/Users/{USERNAME}/Desktop/jboss7
Afterward, exit the terminal and open it again when executing echo $JBOSS_HOME I get nothing.
Apple has changed the default shell to zsh. Therefore you have to rename your configuration files. .bashrc is now .zshrc and .bash_profile is now .zprofile.
If you for some reason (as me) don't want to rename/move your ~/.bash_profile file you can do the next things:
Create a new file ~/.zprofile
Type there source ~/.bash_profile
Save and close
Run a new terminal session
You can just copy your existing bash_profile and name it zprofile and it will work fine.
Run the below command in terminal and you are set after closing and opening new terminal.
cp ~/.bash_profile ~/.zprofile
I created a new file called
/usr/local/bin/mybash
which contains a wrapper script:
/usr/local/bin/bash --init-file $HOME/.bashrc
I installed this local/bin/bash from HomeBrew.
Full Sequence of Events
brew install bash
echo "/usr/local/bin/bash --init-file $HOME/.bashrc" > /usr/local/bin/mybash
chmod +x /usr/local/bin/mybash
Then I opened the settings for terminal.app [cmd-comma]. Under the General Tab, select the radio button for Command (complete path)
In the text box change the text from /bin/zsh/ to /usr/local/bin/bash.
After you close a Terminal window, variables you set in that window are no longer available. If you want the value of a variable to persist across sessions and in all Terminal windows, you must set it in a shell startup script. For information about modifying your zsh shell startup script to keep variables and other settings across multiple sessions, see the “Invocation” section of the zsh man page.
You can use ~/.zlogin to add your variables.
Check out this reference.
changing the bash profile to zsh profile works and source it as well to see in action.
vikas#Vikas-Kumar ~ % mv .bash_profile .zsh_profile
vikas#Vikas-Kumar ~ % source .zsh_profile
You can create a simbolic link and keep your .bash_profile file with this:
ln -s .bash_profile .zsh_profile
source .zsh_profile
Any changes in .bash_profile will be reflected in .zsh_profile
Even with os Catalina /bin/bash comes for free, brew is not needed. Simply create your .bash_profile and set shell in terminal settings to /bin/bash. it automatically finds your .bash_profile. z-shell is not bash-shell and simply renaming will work in most cases but definitely is not correct.
you don't need to update the file, zsh is mac's default, put this in terminal. e.g.:
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools
cp zprofile ~/.zprofile
Add to .zprofile:
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
eg. by >vi .zprofile
Done

Why must I source .bashrc every time I open terminal for aliases to work?

Git was working fine. I have created an alias in Git but the issue is when I tried to reopen the terminal, then I need to run . ~/.bashrc every time in the terminal.
What is the best way I don't need to provide source every time when I reopen the terminal?
What I did?
I am trying to add source of the .bashrc file in this file but it is a read-only file. I am not able to add the source of the .bashrc file in this profile.
open /etc/profile
Added the permission to write in the profile as well, still not able to link the source file.
sudo chmod u+w /etc/profile
Profile:
# System-wide .profile for sh(1)
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
if [ "${BASH-no}" != "no" ]; then
[ -r /etc/bashrc ] && . /etc/bashrc
fi
It looks like your terminal emulator is launching bash as a login shell.
If that's the case, it will read /etc/profile for configuration as well as 1 of the following files, if they exist (listed in order of importance) :
~/.bash_profile
~/.bash_login
~/.profile
It will thus ignore your .bashrc file. A correct fix for your situation would be to either configure your terminal emulator to run bash interactively and non-login, or add the following line to your ~/.bash_profile :
[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"
Here is a link to the documentation about which files are loaded depending of the type of shell you are running
As per #Aserre's answer i have followed this step to solve this issue
A typical install of OS won't create a .bash_profile for you. When you want to run functions from your command line, this is a must-have.
Start up Terminal
Type cd ~/ to go to your home folder
Type touch .bash_profile to create your new file.
Edit .bash_profile with your favorite editor (or you can just type open -e .bash_profile to open it in TextEdit.
[ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc" Save it and close it
Restart the terminal, It should work
You should write this line source .profile inside your .zshrc file. This is because default shell is zsh. If u don't want to do this solution than u can go for changing the default shell by typing the following command chsh -s /bin/bash then restart your machine or virtual machine. Then no need for source. I hope this will help :) TAKE CARE
If you are using Linux and you want variables set, to persist.
Follow the below steps.
Be the root user -> sudo su
go to etc folder -> cd /etc
open the file bashrc with the editor of your choice -> vi bashrc
set the variable with export command like here I am setting JAVA_HOME ->
export JAVA_HOME=pathHere
Load the bashrc file with command ->
. bashrc
remember to put the dot/period before bashrc.
now JAVA_HOME should be set permanently.
Thanks...

alias script is not running `source ~/.bash_profile`

I have modified my .bash_profile script with the following command:
alias shortcuts-update='nano ~/.bash_profile && source ~/.bash_profile && say profile shortcuts have been updated'
It's a great way to add aliases on the fly. The nano works well but the the source ~/.bash_profile does not! I must run it manually before any changes I have saved in .bash_profile take effect. Can anyone tell me why?

Mac Terminal error: -bash: /Users/tim/.profile: No such file or directory

Every time I open a new Terminal window I see the following.
-bash: /Users/tim/.profile: No such file or directory
I have no idea why this is happening or where to look to fix it; my profile is located at /Users/tim/.bash_profile not /Users/tim/.profile
Any thoughts on how to troubleshoot this?
This happened to me as well and I noticed that at the top of my .bash_profile it had:
source ~/.profile
My understanding is that on MacOS / OS X .bash_profile is given priority over .profile so the latter file isn't required.
To fix therefore I simply removed source ~/.profile from the top of the file.
Hope that helps.
Edit: original poster commented that he just created a blank .profile to solve this as well.
I met the same issue today, i guess it's because i use the cleaning app and the app considered this file was a waste of space so deleted it. touch ~/.profile helps me.
I encountered this issue after I switched to an M1 chip MacBook. I found the solution, follow these steps should fix the issue:
1st: Add below codes to ~/.bash_profile (simply run this code in Terminal)
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
2nd: Run the below codes in Terminal
source ~/.bash_profile
This should fix the issue, you can check nvm -v. However, if the issue persists, try the below steps:
Open the ~/.zshrc file, and add the below codes at the top of the file and save.
source ~/.bash_profile
source ~/.zshrc
(if you don't know how to access the ~/.zshrc add whatever code editor in front of it in Terminal, I use VS code thus code ~/.zshrc).
This should fix the issue.
Simply open .bash_profile file and comment on the first line.
in my case
source ~/.profile
export PATH="/usr/local/sbin:$PATH"
to
#source ~/.profile
export PATH="/usr/local/sbin:$PATH"
Another way you just create the file in same folder where
.bash_profile exists.
But the first one is good if you don't need .profile for any purpose
My problem was i didn't have "" when setting variables.
I changed
export TEST=SOME_VAR
to
export TEST="SOME_VAR"
and it was working again
"
When you start a Terminal window, the shell, zsh, executes the code in ".zprofile" and this is trying to access a file called ".profile".
You need to edit the .zprofile to remove these offending lines.
If you are unsure about what this code is doing, you should probably not make changes to it.
Instead, I suggest you do this: rename this file, and then see if things are working normally.
In more detail:
a) In Terminal, rename .zprofile to something different, for example ".zprofile-inactive":
mv .zprofile .zprofile-inactive
b) Quit Terminal, and start it again "

Resources