Mayank Patel
Mayank Patel

Reputation: 3908

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

Upvotes: 9

Views: 26082

Answers (4)

Debayan
Debayan

Reputation: 499

If you are using Linux and you want variables set, to persist. Follow the below steps.

  1. Be the root user -> sudo su
  2. go to etc folder -> cd /etc
  3. open the file bashrc with the editor of your choice -> vi bashrc
  4. set the variable with export command like here I am setting JAVA_HOME ->
export JAVA_HOME=pathHere
  1. Load the bashrc file with command ->
. bashrc

remember to put the dot/period before bashrc. now JAVA_HOME should be set permanently. Thanks...

Upvotes: 0

Aserre
Aserre

Reputation: 5062

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

Upvotes: 24

Vansh Verma
Vansh Verma

Reputation: 21

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

Upvotes: 1

Mayank Patel
Mayank Patel

Reputation: 3908

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.

  1. Start up Terminal
  2. Type cd ~/ to go to your home folder
  3. Type touch .bash_profile to create your new file.
  4. Edit .bash_profile with your favorite editor (or you can just type open -e .bash_profile to open it in TextEdit.
  5. [ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc" Save it and close it

Restart the terminal, It should work

Upvotes: 13

Related Questions