Skip to content

Instantly share code, notes, and snippets.

@jz709u
Last active July 23, 2020 17:03
Show Gist options
  • Save jz709u/9331d26619f02185ca6c0df3a668c6e6 to your computer and use it in GitHub Desktop.
Save jz709u/9331d26619f02185ca6c0df3a668c6e6 to your computer and use it in GitHub Desktop.
.bash_profile, .bashrc #jz709u #bashrc #bash_profile #bash

When you start bash as an interactive shell (i.e., not to run a script), it reads ~/.bashrc (except when invoked as a login shell, then it only reads ~/.bash_profile or ~/.profile).

  • ~/.profile is the place to put stuff that applies to your whole session, such as programs that you want to start when you log in (but not graphical programs, they go into a different file), and environment variable definitions.

  • ~/.bashrc is the place to put stuff that applies only to bash itself, such as alias and function definitions, shell options, and prompt settings. (You could also put key bindings there, but for bash they normally go into ~/.inputrc.)

  • ~/.bash_profile can be used instead of ~/.profile, but it is read by bash only, not by any other shell. (This is mostly a concern if you want your initialization files to work on multiple machines and your login shell isn't bash on all of them.) This is a logical place to include ~/.bashrc if the shell is interactive. I recommend the following contents in ~/.bash_profile:

  • if [ -r ~/.profile ]; then . ~/.profile; fi case "$-" in *i*) if [ -r ~/.bashrc ]; then . ~/.bashrc; fi;; esac

Login and non-Login Shell

When you login (eg: type username and password) via console, either physically sitting at the machine when booting, or remotely via ssh: .bash_profile is executed to configure things before the initial command prompt.

But, if you’ve already logged into your machine and open a new terminal window (xterm) inside Gnome or KDE, then .bashrc is executed before the window command prompt. .bashrc is also run when you start a new bash instance by typing /bin/bash in a terminal.

Reference

https://medium.com/@abhinavkorpal/bash-profile-vs-bashrc-c52534a787d3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment