What is the difference between ~/.profile and ~/.bash_profile?
-
1possible duplicate of What's the conf file reading between login and non-login shell?Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2012-08-17 13:39:54 +00:00Commented Aug 17, 2012 at 13:39
-
1And for the other part of your question, see unix.stackexchange.com/questions/3052/alternative-to-bashrcGilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2012-08-17 13:40:18 +00:00Commented Aug 17, 2012 at 13:40
4 Answers
The .profile was the original profile configuration for the Bourne shell (a.k.a., sh). bash, being a Bourne compatible shell will read and use it. The .bash_profile on the other hand is only read by bash. It is intended for commands that are incompatible with the standard Bourne shell.
-
1If i am wrong, do correct me.. .profile is used by any Bourne compatible shell whereas .bash_profile is used by bash only.. am i right?lakshmen– lakshmen2012-08-17 05:08:11 +00:00Commented Aug 17, 2012 at 5:08
-
6@lakesh: Yes, any shell providing bourne compatibility will read
.profile. E.g.,bashandkshbut notcshortcsh. Andzshprovides bothshandcshcompatibility so it will read both.profileand.login, as well aszshspecific dot files.bahamat– bahamat2012-08-17 07:59:03 +00:00Commented Aug 17, 2012 at 7:59 -
is there any tutorial to read up on this bash and ksh stuff? never heard of these before...lakshmen– lakshmen2012-08-17 08:02:17 +00:00Commented Aug 17, 2012 at 8:02
-
5You could start with the UNIX Shell History.bahamat– bahamat2012-08-17 16:12:27 +00:00Commented Aug 17, 2012 at 16:12
-
1@bahamat in my testing, and according to this gnu doc,
~/.profileis only read byshif/etc/profiledoes not exist (note myshis invokingbash).SpinUp __ A Davis– SpinUp __ A Davis2021-03-15 23:47:39 +00:00Commented Mar 15, 2021 at 23:47
The original sh sourced .profile on startup.
bash will try to source .bash_profile first, but if that doesn't exist, it will source .profile1.
Note that if bash is started as sh (e.g. /bin/sh is a link to /bin/bash) or is started with the --posix flag, it tries to emulate sh, and only reads .profile.
Footnotes:
- Actually, the first one of
.bash_profile,.bash_login,.profile
See also:
-
A small point about
bashstarted assh:~/.profilewill only be read if/etc/profiledoes not exist. This gnu doc explains it all in agonizing detail.SpinUp __ A Davis– SpinUp __ A Davis2021-03-15 23:43:54 +00:00Commented Mar 15, 2021 at 23:43 -
so if
.bash_profileexists,.profileis not read?a06e– a06e2022-10-23 08:41:50 +00:00Commented Oct 23, 2022 at 8:41 -
@a06e Correct (unless
.bash_profiledoes something likesource .profile).ikegami– ikegami2024-02-23 15:46:17 +00:00Commented Feb 23, 2024 at 15:46
You know many shells exist in the UNIX world, but most of them are:
- Bourne shell:
/bin/sh(Inventor: Stephen Bourne) - BASH (Bourne Again Shell):
/bin/bash(Inventor: Brian Fox, under GNU project) (powerful shell) - C shell:
/bin/csh(Inventor: Bill Joy, Inventor of TCP/IP Stack) - Korn shell:
/bin/ksh(Inventor: David Korn under Bell Labs) - Z shell:
/bin/zsh(Powerful shell) - TENEX C shell:
/bin/tcsh(derived from C Shell) - Debian Almquist shell:
/bin/dash(Derived from Almquist shell (ash under NetBSD project)) (Dash born from lenny)
But your question is about ~/.bash_profile and ~/.profile:
When you you log in to a UNIX machine, it redirects to your home directory, according to the shell chosen by an administrator in the last field of /etc/passwd such as :
mohsen:x:1000:1000:Mohsen Pahlevanzadeh,,,:/home/mohsen:/bin/bash
Your shell runs, and by default each shell has a set file for login and logout. When you log in on bash, ~/.profile is run and when you logout, ~/.bash_logout is run.
~/.bash_history file keeps your input command.
Initialization file in each shell
TENEX C shell
~/.loginWhen you login~/.logoutWhen you logout~/.tcshrcsame as~./bashrcin bash
You can set variable $histfile as name of history file and variable $history as number of commands to keeping.
Z shell
Indeed it's powerful shell and if you get free time, be sure migrate to it.
Except of other shell, Z shell has many configuration file and initialization files, just i write:
$ZDOTDIR/.zshenv
$ZDOTDIR/.zprofile
$ZDOTDIR/.zshrc
$ZDOTDIR/.zlogin
$ZDOTDIR/.zlogout
/tmp/zsh*
/etc/zshenv
/etc/zprofile
/etc/zshrc
/etc/zlogin
Note: if $ZDOTDIR unset, home set.
C shell
Note: TENEX C shell was forked from C shell. C shell supports by BSD. If you are familiar with C language programing, you should be comfortable since its syntax is similar.
~/.login
~/.cshrc
~/.logout
Note: csh is old. Use tcsh instead.
Korn Shell
~/.profile- rc file: user defined
- logout file: N/A
Bourne Again SHell (BASH)
It's very very powerful shell and born under GNU project and forked by Bourne Shell.
~/.bash_login
~/.bash_logout
~/.bashrc
~/.bash_profile
~/.bash_history
When you login, bash runs ~/.bash_profile and ~/.bash_profile runs ~/.bashrc. Indeed ~/.bashrc isn't bash initialization file, because bash doesn't run it.
Bourne shell
It dead. Even when you use man sh, you see manual of dash. [Editor's note: the bit about dash only applies to Debian and Debian-based distros like Ubuntu.]
Your Answer
~/.bash_profile work under bash, but ~/.profile work under Bourne and Korn shell.
-
2Chet Ramey was the main developer for Bash.tripleee– tripleee2013-01-31 10:19:38 +00:00Commented Jan 31, 2013 at 10:19
A login shell is simply a shell you can login as via it ssh or at the console. A non-login shell is a shell that someone can not login too. A non-login shell is often used by programs/system services.
As for your third point. It is true .bashrc is executed on each instance of the shell. However .bash_profile is only used upon login. Thus the reason for the two separate files.
.profile is for things that are not specifically related to Bash, like environment variables $PATH it should also be available anytime. .bash_profile is specifically for login shells or shells executed at login.
-
difference between ~/.profile and ~/.bash_profile?lakshmen– lakshmen2012-08-17 04:46:14 +00:00Commented Aug 17, 2012 at 4:46
-
2.profile is for things that are not specifically related to Bash, like environment variables PATH it should also be available anytime. .bash_profile is specifically for login shells or shells executed at login.anzenketh– anzenketh2012-08-17 04:54:21 +00:00Commented Aug 17, 2012 at 4:54
-
add that statement to your answer.... cos that what's my question is....lakshmen– lakshmen2012-08-17 04:57:58 +00:00Commented Aug 17, 2012 at 4:57
-
Huh? That doesn't even answer the question?Mikel– Mikel2012-08-17 15:32:21 +00:00Commented Aug 17, 2012 at 15:32