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?
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.
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 ~/.
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.
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.
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
.
+-----------------+ | | interactive shell -->| ~/.bashrc | | | +-----------------+
interactive shell
will source ~/.bashrc
automatically.
Take a look at Should the .bashrc in the home directory load automatically?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With