본문 바로가기
Linux

Difference between /.bashrc, /.profile and /.bash_profile

by 공대우냉이 2016. 1. 7.

profile과 bashrc의 가장 큰 차이점은 그 목적이다. 

profile은 터미널에 접속할때 환경 설정을 위한 목적으로 이용되고 터미널에서 실행하는 모든 프로그램에 적용된다.

bashrc도 마찬가지고 터미널에 접속할때 실행되어 적용되지만 주로 터미널에서의 alias and function definitions, shell options, and prompt settings를 목적으로 한다.

alias는 아마도 대체 명령어를 말하는 것 같다. 예를 들어 echo라는 명령어를 print 라는 명령어로 대체해서 쓸 수 있게 하는 것을 의미하는것 같다.

밑에 글은 원본이고 제가 잘못 이해한 부분은 comment로 달아서 수정할 수 있게 해주시기 바랍니다.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Traditionally, when you log into a Unix system, the system would start one program for you. That program is a shell, i.e., a program designed to start other programs. It's a command line shell: you start another program by typing its name. The default shell, a Bourne shell, reads commands from ~/.profile when it is invoked as the login shell.

Bash is a Bourne-like shell. It reads commands from ~/.bash_profile when it is invoked as the login shell, and if that file doesn't exist¹, it tries reading ~/.profile instead.

You can invoke a shell directly at any time, for example by launching a terminal emulator inside a GUI environment. If the shell is not a login shell, it doesn't read ~/.profile. 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.

Therefore:

  • ~/.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:

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

additional thing : what is login shell?

According to the bash man page, .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

What is a login or 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 : http://superuser.com/questions/183870/difference-between-bashrc-and-bash-profile/183980#183980