LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-26-2007, 08:27 AM   #1
muazfarooqaslam
LQ Newbie
 
Registered: Dec 2007
Posts: 22

Rep: Reputation: 15
Diff between /etc/profile and ~/.bash_profile


Hi guys,

I am new to linux and despite of searching alot on google still confused about following basic concepts,

1. What is the difference between /etc/.profile and ~/bash_profile? If i make changes in /etc/profile, it will be implemented system wide for all users and all shells but changes in ~/bash_profile will be specific to that specific user only, is this correct?

2. If changes are made in ~/bash_profile of a user but default shell of user is not bash then nothing will happen, correct? In other words changes in ~/bash_profile will only affect bash shell of that specific user.

3. Suppose i have a script (/tmp/abc) and i want this script to run each time a specific user login, should i add following line in his ~/bash_profile,

sh /tmp/abc

but again if the default shell of that user is not bash, then nothin will happen, right?

4. For same script (/tmp/abc), i want it to run each time any user login, should i add following line in /etc/profile,

sh /tmp/abc

is this correct?

5. What is the difference between ~/.bashrc and ~/.bash_profile?

6. For achieving point 4, can i proceed like this. Add this script to rc(x).d directory of default runlevel?

7. What is the difference between login and non-login shells?

8. What is the difference between interactive and non-interactive shells?

I will be really thankful to anyone who can help me with these points and this thread will certainly help alot of newbies like me.

Thanks
 
Old 12-26-2007, 11:49 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,476

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
Quote:
1. What is the difference between /etc/.profile and ~/bash_profile? If i make changes in /etc/profile, it will be implemented system wide for all users and all shells but changes in ~/bash_profile will be specific to that specific user only, is this correct?
Yes (but it's not /etc/.profile, it's /etc/profile).
Quote:
2. If changes are made in ~/bash_profile of a user but default shell of user is not bash then nothing will happen, correct? In other words changes in ~/bash_profile will only affect bash shell of that specific user.
Yes.
Quote:
3. Suppose i have a script (/tmp/abc) and i want this script to run each time a specific user login, should i add following line in his ~/bash_profile,

sh /tmp/abc

but again if the default shell of that user is not bash, then nothin will happen, right?
Yes
Quote:
4. For same script (/tmp/abc), i want it to run each time any user login, should i add following line in /etc/profile,

sh /tmp/abc

is this correct?
Yes (but you probably ought to avoid doing so unless absolutely necessary for some really, really good reason).
Quote:
5. What is the difference between ~/.bashrc and ~/.bash_profile?
The "profile" is, traditionally, for setting PATH names and the like. The "rc," also traditionally, are for setting user-specific environment variables; e.g., aliases and the like ("rc" stands for "run com" [as in "command"], the way things were done by DEC back in the day). When the login program runs, it first executes the content of /etc/profile, then a local profile then any rc files.
Quote:
6. For achieving point 4, can i proceed like this. Add this script to rc(x).d directory of default runlevel?
You can but you probably don't want to: handle stuff in a profile that gets executed when a log in happens.
Quote:
7. What is the difference between login and non-login shells?
Uh, well, "login" is at login, non-login is when you execute a program (the shell forks-and-execs a new shell (the child) that inherits the environment of the login shell (the parent); that shell "lives" only until the program you executed exits). There's more to it than that, but, well, Google is your friend.
Quote:
8. What is the difference between interactive and non-interactive shells?
Exactly and precisely that (again, Google is your friend), but essentially, a shell running a shell program is a non-interactive shell.
 
Old 12-26-2007, 11:50 AM   #3
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Rep: Reputation: 30
muazfarooqaslam,

Here is my best answers to your questions. I am sure there are some corrections to be made so if a guru can chime in we all can learn.


1. /etc/profile contains (variables, environment settings) that once modified affect system wide changes that will affect all users, ~/.bash_profile is an personal "/etc/profile" in a sense. It contains your custom variables, environment settings. Changes to .bash_profile override /etc/profile for a user. So if I set a variable in .bash_profile called "TREE=green" and there is a variable called "TREE=red" in /etc/profile my "TREE=green" overrides because .bash_profile is interpreted.


2. Yes, bash_profile is for the bash shell, if your using another shell I don't believe these variables and environment settings apply. Other shells might not be able to interpret the Bash syntax. If bash_profile and is missing then other shells will use ~./profile.

3. Yes it will run, and it might not be interpreted upon startup by another shell since they don't read .bash_profile.


4. Yes I believe small scripts that are to run globally should be placed into /etc/profile.d, You need to make this directory in the /etc/ directory.

5. ./bash_profile - contains environment variables

./bash_rc - contains system wide aliases and functions

Great resource:

http://tldp.org/LDP/Bash-Beginners-G...ect_03_01.html


6. Yes you can add the script to the /etc/rc.local and it will be ran upon startup or just drop the script in /etc/rc3.d or whatever runlevel of choice. Please someone correct me if I am wrong.


7. A login shell is a shell is read from /etc/passwd to determine what shell you will start out with when you log into the system, A non-login shell is a shell you can switch to after logging in such as if my shell was defined as bash in /etc/passwd I can switch to the korn shell by doing a /bin/ksh


8. An interactive shell is a shell where you manually type in commands such as what your doing when you run commands such as "ls, pwd, cd ../.." etc..

A non-interactive shell is a "shell script" where you place all the commands you want into a file or script such as:

!#/bin/bash

ls
pwd
cd ../..

and then either source the file or execute it. You are non interacting with the shell or manually typing in commands.
 
Old 12-27-2007, 10:46 AM   #4
muazfarooqaslam
LQ Newbie
 
Registered: Dec 2007
Posts: 22

Original Poster
Rep: Reputation: 15
Thanks alot tronayne and Keysorsoze. It really helped. I got following material on initialization files, it also helps straightning concepts,

"There are four files of interest:
/etc/profile
/etc/bashrc
~/.bash_profile
~/.bashrc

The first two files are system-wide configuration files for bash. Anything that you put in them will affect all users on the system. The last two are your user configuration files for bash. Any modifications to these files will only affect your user.

The files /etc/profile and ~/.bash_profile are used when bash is started as a login shell. This usually happens when you log into your X Windows session or log in remotely, using e.g. SSH. Otherwise, the /etc/bashrc and ~/.bashrc files are used. In both cases, the system-wide file is read first, and the user-specific file second.

Setting the PATH variable is usually done in the profile files, since you only want to set those one, not every time you open a new X terminal."
 
Old 12-27-2007, 02:21 PM   #5
codePlate
LQ Newbie
 
Registered: Dec 2007
Posts: 1

Rep: Reputation: 0
Cool ineeda good ksh .profile

Please help me set up my mandrivia everex laptop.

anyone have a good ksh .profile?

its been ~3 years since i played with linux and i really need to search on my laptop.

ty

cP




Quote:
Originally Posted by muazfarooqaslam View Post
Thanks alot tronayne and Keysorsoze. It really helped. I got following material on initialization files, it also helps straightning concepts,

"There are four files of interest:
/etc/profile
/etc/bashrc
~/.bash_profile
~/.bashrc

The first two files are system-wide configuration files for bash. Anything that you put in them will affect all users on the system. The last two are your user configuration files for bash. Any modifications to these files will only affect your user.

The files /etc/profile and ~/.bash_profile are used when bash is started as a login shell. This usually happens when you log into your X Windows session or log in remotely, using e.g. SSH. Otherwise, the /etc/bashrc and ~/.bashrc files are used. In both cases, the system-wide file is read first, and the user-specific file second.

Setting the PATH variable is usually done in the profile files, since you only want to set those one, not every time you open a new X terminal."
 
Old 12-27-2007, 05:10 PM   #6
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,476

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
Well, I don't know how good it is, but this is my user .profile (some of which will be meaningless unless you are a BOINC user, have CVS installed as a "pserver" and have Firefox in /usr/local):
Code:
#       set up default columns and lines
COLUMNS=80
LINES=40
export COLUMNS LINES
#       set up default group
GRPNAME=`groups | cut -d' ' -f1`
export GRPNAME
#       set up a good-size history
HISTSIZE=500
export HISTSIZE
#       set up the ksh environment
ENV=${HOME}/.kshrc
export ENV
#       set up CVSROOT
CVSROOT=:pserver:myid@mybox:/usr/local/cvsroot
export CVSROOT
#       set the BROWSER path for BOINC
export BROWSER="/usr/local/firefox/firefox"
#       change the PATH a little
export PATH=.:${HOME}/bin:${PATH}
#       make COLUMNS and LINES the screen size
eval `resize`
And here's the .kshrc file I use:
Code:
alias lc='/usr/bin/clear; /bin/ls ${LS_OPTIONS} -aCF'
alias ll='/bin/ls ${LS_OPTIONS} -al'
alias cls='clear'
alias env='env | sort'
alias hi='history -${LINES}'
alias rs='eval `resize`'
These are the options I like and have used for years on Solaris and Linux running ksh, for what they're worth...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Diff between /etc/profile and ~/bash_profile muazfarooqaslam Linux - General 1 12-26-2007 08:28 AM
.profile or .bash_profile user52 Linux - Newbie 8 12-02-2006 12:48 PM
.bash_profile and /etc/profile not sourced? FogSwimmer Ubuntu 7 10-05-2006 06:45 AM
/etc/profile VERSUS /home/.bash_profile carbono Linux - Newbie 12 10-12-2005 10:23 AM
profile, .bash_profile & ???? ChimpFace9000 Linux - General 1 07-05-2001 05:52 PM


All times are GMT -5. The time now is 02:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration