LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-08-2017, 10:15 AM   #1
bodisha
Member
 
Registered: Oct 2016
Posts: 32

Rep: Reputation: Disabled
Understanding differences between individual BASH login scripts


Hello... and thanks in advance for reading this or offering me any assistance

I'm trying to understand specific differences between the various login scripts... I understand the differences between interactive vs non-interactive and login vs non-login shells... and that's not where my question is

Where I'm confused is what I've read seemed to allude that certain login scripts would have a certain type of content and others could have a different type of content

I've seen people posting about how one command (like umask) would work in one script but not another and I haven't seen a clear cut definition of which scripts should contain what

I would be very grateful if someone could explain any restrictions or rules with the login script... I'm particularly confused on the purpose & function of the /etc/bashrc, ~/.bashrc, and /etc/environment files... I understand bashrc & .bashrc are used in non-login interactive environments... I'm confused on what their content would be

Anyway... once again thank you if anyone took the time to read this!
 
Old 11-08-2017, 10:53 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 5,594
Blog Entries: 16

Rep: Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319
.bashrc is used by all shells including those you launch by opening an Xterm. .bash_profile is only run by login shells. Traditionally .bashrc is used for command aliases and .bash_profile for setting environmental variables (such as the prompt PS1). But not everyone keeps to that convention. I'm typing this out of Crux and that puts everything including the environment in .bashrc.

Files in /etc, like /etc/profile and /etc/bashrc contain settings applicable to all users. Usually the personal files have a line including the /etc file.
 
Old 11-08-2017, 12:33 PM   #3
bodisha
Member
 
Registered: Oct 2016
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hazel View Post
Traditionally .bashrc is used for command aliases and .bash_profile for setting environmental variables (such as the prompt PS1). But not everyone keeps to that convention.

Thanks for the quick reply... I hope you don't mind a follow up question... please correct me if my understanding isn't correct... the Bash man page says

Quote:
/bin/bash
The bash executable
/etc/profile
The systemwide initialization file, executed for login shells
~/.bash_profile
The personal initialization file, executed for login shells
~/.bashrc
The individual per-interactive-shell startup file
~/.bash_logout
The individual login shell cleanup file, executed when a login shell exits
~/.inputrc
Individual readline initialization file

I'm still a little confused on definition of the man page... The way I'm interpreting your post is to mean is anything can be entered into any of the scripts regardless of their name? It's simply limited by convention and the difference between a file being read as a global or local login script? That there's no real difference between the /etc/bashrc file or the /etc/profile? And I could edit them to include anything... it's simply limited by if a script is a local user script or global one?

I'm also unsure of how /etc/environment is used... from what I've read is it does not do variable expansion... and mostly used to set system wide environment variables... Which has me struggling to understand when I would use that verses the /etc/bashrc file

I apologize if I'm not understanding your point... And I appreciate your patience
Attached Thumbnails
Click image for larger version

Name:	man bash.jpg
Views:	14
Size:	119.8 KB
ID:	26235  
 
Old 11-08-2017, 12:55 PM   #4
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 5,594
Blog Entries: 16

Rep: Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319Reputation: 3319
What the man page says is what I would say too: /etc files for users generally, local . files for individual tweaks, profile/.profile/.bash_profile for logins (when you want environmental variables set once and for all) and bashrc/.bashrc for all interactive shells, whether login shells or not.

But Linux is all about choice and that includes the choice of distro developers to do things differently or even eccentrically. I would treat the man page as the standard if I were you but it's not set in stone.
 
Old 11-09-2017, 02:07 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,426
Blog Entries: 10

Rep: Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249
Quote:
Originally Posted by bodisha View Post
anything can be entered into any of the scripts regardless of their name? It's simply limited by convention
i think you're right there.
they're basically just shell scripts.
but i think that only one (*profile*) is being executed, and the rest are sourced. i could be wrong.
 
Old 11-09-2017, 04:06 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,784

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
None of these files are actually executed otherwise unexported variable settings would be lost among several other instructions and the shell would quickly exit when reaching their end of file.

They might only be sourced.

Last edited by jlliagre; 11-09-2017 at 04:24 AM.
 
Old 11-09-2017, 09:49 AM   #7
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,964

Rep: Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956
/etc/profile and ~/.profile are processed by all POSIX shells, not just bash, therefore it's a good idea to keep any bash specific syntax (bashisms) or bash specific settings (like the PS1 string format extensions) out of these two files as they will cause errors when processed by shells other than bash.

Similarly, the PS1 string should not be exported as that can also cause problems when invoking other shells as a child process of a bash shell. here's an example of that:
Code:
test@ws1:~$ dash
$ exit
test@ws1:~$ ksh
$ exit
test@ws1:~$ export PS1
test@ws1:~$ dash
\[\]\u@\h:\w$ \[\]exit
test@ws1:~$ ksh
[]u@h:w$ []exit
test@ws1:~$
As you can see, neither dash nor ksh like the bash format PS1 string. Sadly, many distro (even my beloved Slackware) get this wrong.


~/.bash_profile is a bash specific equivalent to ~/.profile that bash will run in preference to ~/.profile if it exists. It's safe to put bash specific syntax in that file as only bash will read it.

~/.bashrc is a bash specific startup file that is processed by user interactive shells, but not login shells. Personally, I don't like that it gets skipped, so I always add the following to my ~/.bash_profile:
Code:
case $- in
*i* )  # Interactive shell
       if [ -f $HOME/.bashrc ]; then
          source $HOME/.bashrc
       fi
       ;;
esac
... this way, anything I set in my ~/.bashrc applies to any interactive bash shell regardless of whether it's a login or non-login shell and I don't have to duplicate stuff in both .profile and .bashrc.

/etc/environment isn't a shell-script, and it isn't read by the bash executable. It's actually processed by login(1) or the PAM module on PAM enabled systems. It is just a file containing a list of literal key=value environment variable settings and can contain no logic or variable expansions/substitution of any kind. It's not used that much these days, which is a shame because I always liked the idea of it much better than setting everything from /etc/profile.

Finally, there's also $ENV (which people typically point to something like ~/.shinit or /etc/shinit) and which is the way POSIX shells do something akin to, but not exactly the same as ~/.bashrc.


I hope that sheds some light on the topic. As you can probably tell seeing this stuff done incorrectly is one of my pet hates.

Last edited by GazL; 11-09-2017 at 09:57 AM.
 
2 members found this post helpful.
Old 11-09-2017, 02:34 PM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138
One big difference is that .bash_profile is only sourced once when you first log in, while .bashrc is sourced every time you open a terminal. This means is you make a change in .bashrc you only need to close and re-open the terminal for it to take effect, while if you make a change in .bash_profile you actually need to log out and back in. This also means that if you append to a variable in .bashrc, eg: "export PATH=$PATH:/bin", it will be appended to every time you open the shell, while for .bash_profile it won't. Eg:

Code:
$ cat ~/.bashrc
export TEMPVAR=${TEMPVAR}:append
$ echo $TEMPVAR
:append
$ bash
$ bash
$ bash
$ bash
$ echo $TEMPVAR
:append:append:append:append:append
vs
Code:
$ cat ~/.bash_profile
export TEMPVAR=${TEMPVAR}:append
$ echo $TEMPVAR
:append
$ bash
$ bash
$ bash
$ bash
$ echo $TEMPVAR
:append

Last edited by suicidaleggroll; 11-09-2017 at 02:35 PM.
 
1 members found this post helpful.
Old 11-09-2017, 06:44 PM   #9
bodisha
Member
 
Registered: Oct 2016
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by GazL View Post
Code:
case $- in
*i* )  # Interactive shell
       if [ -f $HOME/.bashrc ]; then
          source $HOME/.bashrc
       fi
       ;;
esac
... this way, anything I set in my ~/.bashrc applies to any interactive bash shell regardless of whether it's a login or non-login shell and I don't have to duplicate stuff in both .profile and .bashrc.
Thanks for the reply! I appreciate it! I hope you don't mind a follow up question

So to clear things up about the /etc/bashrc and ~/.bashrc files... These two files are sourced regardless as long as it's an interactive shell?

I tossed echo commands in all the start up scripts to see what would start up and when... I noticed when I started an interactive shell (Gnome GUI terminal or putty/su command) the /etc/bashrc & ~/.bashrc scripts were sourced in both login & non-login shells

What confused me was the /etc/profile, /etc/bashrc, ~/.bash_profile and ~/.bashrc scripts were all getting sourced in a login interactive shell (putty or su)

Does this seem right? I would expect one to start up only for interactive shells and the other to start up only for non-interactive shells... can you confirm this is how it's suppose to work for me please? I can't see any benefit from sourcing ~/.bashrc & /etc/bashrc in both situations but only sourcing /etc/profile in a login shell

Once again thanks for the reply

Last edited by bodisha; 11-09-2017 at 07:16 PM.
 
Old 11-10-2017, 12:16 AM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,426
Blog Entries: 10

Rep: Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249Reputation: 5249
Quote:
Originally Posted by bodisha View Post
What confused me was the /etc/profile, /etc/bashrc, ~/.bash_profile and ~/.bashrc scripts were all getting sourced in a login interactive shell (putty or su)

Does this seem right?
yes!
hint: it's both login AND interactive.

on my system, /etc/profile and ~/.bash_profile are sourced for login.

and /etc/bash.bashrc and ~/.bashrc are sourced for every new shell (and usually these files have an if statement right at the top, that makes them exit if it's not interactive. but there are things you still might want to get sourced, so those you put above that).
 
1 members found this post helpful.
Old 11-10-2017, 03:59 AM   #11
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,964

Rep: Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956
I used the 'echo' trick myself when I was trying to get to grips with this stuff, long ago.

To the best of my understanding, ~.bashrc doesn't get sourced for a login shell (interactive or otherwise) unless you take steps like I have done above. Perhaps your distro has already put a snippet of code similar to the one I use above in either /etc/profile or one of the members of /etc/profile.d/

Slackware doesn't have a /etc/bashrc. I don't know whether enabling that is a bash build-time configure option, a distro specific patch, or whether it's just being sourced from a command in your ~./bashrc. Perhaps someone else who runs a distro that has a /etc/bashrc will be more familiar with that question.

'su - user' ( traditional equivalent of typing 'su -l user') will start a login shell, running /etc/profile.

'su user' will start a non-login shell, running ~/.bashrc.
(assuming that the user's shell is specified as bash in /etc/passwd of course).

Also of note: some terminal programs allow you to specify in one of their settings whether they start a login or non-login shell when they open, which can also confuse matters. You often see advice offered here and elsewhere when people have issues in this area suggesting that the fix is to make sure that the terminal program starts a login shell. Personally, I think that is a mistake, and is just a case of two wrongs making an almost-but-not-entirely-right.

Last edited by GazL; 11-10-2017 at 04:15 AM.
 
1 members found this post helpful.
Old 11-10-2017, 04:08 AM   #12
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,644

Rep: Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615
if you are lazy (to read) you can insert
Code:
echo this is .bashrc
echo this is /etc/profile
and/or similar lines into anywhere you wish...
 
Old 11-10-2017, 04:13 AM   #13
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,964

Rep: Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956Reputation: 3956
@pan, he mentioned he'd done the echo trick (post #9)
 
Old 11-10-2017, 05:44 AM   #14
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,644

Rep: Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615
Quote:
Originally Posted by GazL View Post
@pan, he mentioned he'd done the echo trick (post #9)
looks like I was lazy...
sorry
 
Old 11-10-2017, 07:59 AM   #15
bodisha
Member
 
Registered: Oct 2016
Posts: 32

Original Poster
Rep: Reputation: Disabled
I would like to thank all of you for replying! It's been a great help... Unfortunately, I am still confused about the similarities/differences of the /etc/profile & /etc/bashrc.

I've looked through them both to try and understand them. At the top of both /etc/profile & /etc/bashrc it says something to the effect of "System wide functions and aliases goes in /etc/bashrc, Environment stuff goes in /etc/profile". So I was expecting them to contain distinctly different content. I found they contained a surprising amount of duplicated work. Which is where I started getting confused.

In both files they have code to execute the files in the /etc/profile.d directory, they both set the umask, and they both create a function that manipulates the $PATH.

Like I said I was expecting both files to have completely different purposes... Especially when there's comments at the beginning of both files that pretty much states they have different purposes

If it clears anything up... The distro I'm working with is Centos 7 and the /etc/bashrc file is sourced by the ~/.bashrc, and the ~/.bashrc is source3d by the ~/.bash_profile. So the bashrc files always run when a user logs in

Anyway... Everyone has been a great help and a lot of my confusion is gone... I'm just kind of hung up on this one point! Thanks!

Last edited by bodisha; 11-10-2017 at 09:09 AM.
 
  


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
Executing bash scripts. Differences beetween ./ and . ./ barbac Linux - Newbie 1 06-18-2015 10:08 AM
Understanding file size differences across filesystems someshpr Linux - General 3 03-20-2015 09:09 PM
Understanding the key differences between LXC and Docker jeremy Linux - News 0 09-12-2014 11:50 AM
[SOLVED] Can anyone help me with fully understanding and organizing my bash startup scripts? Bowlslaw Slackware 2 08-07-2011 03:20 PM
BASH Shell Differences: Login or Interactive theKbStockpiler Programming 10 02-21-2011 01:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:21 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
Open Source Consulting | Domain Registration