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

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...

Related

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

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?

.bashrc not sourced on Intellij IDEA's terminal

I have some aliased defined on my .bashrc that I'd like to use on my Intellij IDEA's terminal. Why is .bashrc not sourced?
.bashrc is only loaded when Bash is running in "interactive" mode. For some reason, IntelliJ defaults to running Bash in non-interactive mode, but this is easy to change. Open the IntelliJ Settings window, then open "Tools -> Terminal", and add -i to the Shell path.
(Note that in this screenshot, I have also changed the default shell, because I'm on a Mac, which makes it difficult to update /bin/bash. Unless you have installed a different version of Bash, do not blindly copy this change!)
Had the same issue with IntelliJ.
Solved this by setting the value /bin/bash --login in setting->Tools->Terminal->Shell path.
/bin/bash --login forces the bash to read the ~/.bash_profile.
https://intellij-support.jetbrains.com/hc/en-us/community/posts/205437150-Terminal-not-sourcing-bash-profile?page=1#community_comment_360000167679
In your home directory, add theses lines to .profile (create the file if it does not exist), in order to source .bashrc:
if [ "$SHELL" = "/bin/bash" ]; then
. ~/.bashrc
fi
For me, changing the line
Exec="/opt/idea-IU-183.4284.148/bin/idea.sh" %f
to
Exec=bash -ic "/opt/idea-IU-183.4284.148/bin/idea.sh" %f
worked. We exploit the hack that the interactive shell loads .bashrc :)
On Mac OSX Catalina, "/bin/bash" and ~/.zprofile worked for me:
Update:
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.
My settings are in ~/.bash_profile . I solved it using:
echo "source ~/.bash_profile" >> .profile
sudo echo ". ~/.bashrc" >> /etc/bash.bashrc
Because > operator doesn't pass the sudo permissions to the write process.
I did this :
echo ". ~/.bashrc" | sudo tee /etc/bash.bashrc
I noticed that .bashrc isn't sourced only when I first install IntelliJ and run it directly via the idea.sh script in bin/.
Once I create the desktop entry via Tools -> Create Desktop Entry... and start it from the Ubuntu dash, .bashrc is sourced properly.
If you have recently moved to zsh from bash then go to ~/.zshrc file and update $PATH variable there:
Default value in .zshrc:
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
Change to same PATH variable set in .bashrc and uncomment it
# If you come from bash you might have to change your $PATH.
export PATH=$HOME/bin:/usr/local/bin:<my-bash-paths>:$PATH
None of these answers worked for me. What did work is
sudo nano /etc/environment
...then manually adding my export and alias commands here to make them system wide.
But be careful, do NOT mess up the PATH or you'll have trouble logging back in to your desktop environment, or many other issues.
I had this problem because the default shell had been changed to zsh.
I echoed the shell name with echo $SHELL to see this (thanks How to get default shell).
Then I changed it back to /bin/bash with this command: chsh -s /bin/bash (thanks https://apple.stackexchange.com/a/100476/176809).
Create a bash script with the content;
echo "source ~/.bash_profile" >> .profile
Then in Intellij go to preferences/tools/Startup Tasks;
create a run configuration that runs your bash script and you're good to go.
I just restarted my MAC and it picked up the new stuff.
For me at least, the only thing that works was to put this in the Shell path variable inside of Tools > Terminal :
/bin/bash --rcfile ~/.bashrc

Issue with bashrc file

When I open the .bashrc file using gedit, or vi editor, there's nothing in it. I am on MINT trying to install Hadoop and one of the step requires editing the .bashrc file, to save the environment variable. I remember doing the same on Ubuntu, and there were contents were in it.
Can you help me get them all back? Is it possible to get them all back?
When I do gedit ~/.bashrc it opens to a blank page.
Thanks in advance!
From looking at the MINT forums, it doesn't look like MINT supplies a default .bashrc:
http://forums.linuxmint.com/viewtopic.php?f=90&t=130358
However, .bashrc is an optional file, so you can add whatever you want to it. Check out the bash man pages:
man bash
Also, if you setup a .bashrc file, make sure it is getting called from either .profile or .bash_profile like this:
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
You should have one in here:: /etc/skel/.bashrc
Try copying it to your home folder:: cp /etc/skel/.bashrc ~/.bashrc
Look for .bashrc.swap files in your home directory by doing so:
ls -Fa ~ | grep '*bashrc*'
I don't know how gedit or other programs name their swap files, but just look what comes up.
If there are no such files, you're out of luck.

Appending bash alias to .bashrc doesn't work

I want to create a alias of my cd command. I have created the .bashrc file and append the command cd ...... to it. (Since the file was newly created, it just has this one line that I added).
After that, only after I typed . ~/.bashrc, can the alias works. If I close the terminal and open it again, I need to retype . ~/.bashrc.
It's really annoying to do this every time. Is there any way to solve this problem?
Thank you so much for help
When you login to linux system, only ~/.profile will be called:
$ cat ~/.profile
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
You need to source ~/.bashrc inside ~/.profile manaully.
Read this to learn more.
EDIT:
If you're using iTerm2 on mac, it actually start a login shell by default when open tabs.
But you can change it: Preferences > General > Command
If using OS X, append the alias to ~/.bash_profile.
You could also add alias to ~/.bashrc, then add source ~/.bashrc to ~/.bash_profile.
Better yet, put all your aliases in ~/.aliases, and source it in ~/.bash_profile.
By default, OS X first sources /etc/bashrc (which shouldn't be modified unless absolutely necessary), then sources the user's ~/.bash_profile at the start of every interactive session.

Resources