Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have to keep using `source ~/.profile` to get settings in place?

Tags:

I have a couple of bash scripts that I want to make sure runs by default and I'm currently storing them in ~/.profile on my mac. Is that the wrong place to be storing them? I've heard of others and tried them (like ~/.bashrc, ~/.bash_profile, etc), but they don't seem to be working.

What is the difference between all of these and which one do I put the scripts in so that it configures on runtime and I don't have to call $ source ~/.profile every time I open the terminal?

like image 394
locoboy Avatar asked Apr 25 '12 17:04

locoboy


People also ask

Why I have to source bashrc every time?

In short, you are putting your aliases in the wrong file . bashrc , that is why you need to keep running source to get the aliases working in any new login terminal instances.

Why do I have to source .profile every time Ubuntu?

The reason for this is that ~/. profile is only sourced by login shells. When you open a new terminal window, the shell that starts is a non-login shell by default. If you log out, and log back in, the change to ~/.

How do you make a source permanent?

But to answer you question: To make your changes permanent, you need to do nothing but login once again. source ~/. profile won't be needed any more then.

What is the function of the ~/ profile file?

The . profile file contains your individual profile that overrides the variables set in the /etc/profile file. The . profile file is often used to set exported environment variables and terminal modes.


2 Answers

If both ~/.bash_profile and ~/.profile exist, bash only reads ~/.bash_profile when it is invoked as an interactive login shell.

https://www.gnu.org/s/bash/manual/html_node/Bash-Startup-Files.html:

Invoked as an interactive login shell, or with --login

When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

[...]

Invoked as an interactive non-login shell

When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.

~/.profile is also used by other shells.

Terminal and iTerm open new shells as login shells by default (by executing something like login -pf $USER), but many GNU/Linux terminal applications open new shells as non-login shells. OS X users often use ~/.bash_profile instead of ~/.bashrc.

like image 193
Lri Avatar answered Sep 29 '22 05:09

Lri


                     +-----------------+                      |                 | interactive shell -->|  ~/.bashrc      |                      |                 |                      +-----------------+ 

interactive shell will source ~/.bashrc automatically.

Take a look at Should the .bashrc in the home directory load automatically?

like image 44
kev Avatar answered Sep 29 '22 05:09

kev