Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to know the difference between ~/.profile and ~/.bash_profile.

share|improve this question
1  
possible duplicate of What's the conf file reading between login and non-login shell? –  Gilles Aug 17 '12 at 13:39
1  
And for the other part of your question, see unix.stackexchange.com/questions/3052/alternative-to-bashrc –  Gilles Aug 17 '12 at 13:40

4 Answers 4

up vote 10 down vote accepted

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.

share|improve this answer
    
If 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? –  lakesh Aug 17 '12 at 5:08
    
@lakesh: Yes, any shell providing bourne compatibility will read .profile. E.g., bash and ksh but not csh or tcsh. And zsh provides both sh and csh compatibility so it will read both .profile and .login, as well as zsh specific dot files. –  bahamat Aug 17 '12 at 7:59
    
is there any tutorial to read up on this bash and ksh stuff? never heard of these before... –  lakesh Aug 17 '12 at 8:02
1  
You could start with the UNIX Shell History. –  bahamat Aug 17 '12 at 16:12

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:

  1. Actually, the first one of .bash_profile, .bash_login, .profile

See also:

share|improve this answer
    
Awesome.. Thanks...especially for the references... –  lakesh Aug 17 '12 at 16:04

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.

share|improve this answer
    
difference between ~/.profile and ~/.bash_profile? –  lakesh Aug 17 '12 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 Aug 17 '12 at 4:54
    
add that statement to your answer.... cos that what's my question is.... –  lakesh Aug 17 '12 at 4:57
    
Huh? That doesn't even answer the question? –  Mikel Aug 17 '12 at 15:32

You know many shell exist in UNIX world, but most of them are:

Bounre shell == /bin/sh => inventor : Stephon 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 sehll)

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 about ~/.bash_profile and ~/.profile:

When you you login in UNIX machine redirect to your home directory, According to your shell that administrator chose for you in last field of /etc/passwd such as :

mohsen:x:1000:1000:Mohsen Pahlevanzadeh,,,:/home/mohsen:/bin/bash

Your shell will be run and behave for you, By default each has a set file for login and logout when you login in bash shell , ~/.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:

~/.login = > When you login
~/.logout = > When you logout
~/.tcshrc  = > same as ~./bashrc in bash shell
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 i 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 forked from C Shell, C Shell supports by BSD. If you familiar with C language programing,So you believe its syntax such as that.

    ~/.login
    ~/.cshrc
    ~/.logout
Note : if you need to it, use tcsh and csh is old but live.

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.

Your Answer:

~/.bash_profile work under bash shell, but ~/.profile work under bourne and korn shell

share|improve this answer
    
Chet Ramey was the main developer for Bash. –  tripleee Jan 31 '13 at 10:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.