Understanding the difference 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!

First off, these files are shell scripts, so they do whatever their author wanted. This is responsible for a lot of the confusion - /etc/bashrc is not a file bash will load unless something else tells it to, but someone could easily have put . /etc/bashrc into /etc/profile for the same effect. You have to read these profile scripts to see what they do, no other way to know.

man bash

...

FILES
...
       /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
...

These are the files bash loads and when it loads them. Anything else is a command someone dropped into one of those files.

Imagine this script:

#!/bin/bash

cd /etc

Set the file executable and run it with ./changedir.sh and it will create a new shell, change directory to /etc/ in that new shell, and die, leaving you in your original shell which is still wherever you left it.

If you run that script with . changedir.sh on the other hand - note the space - that instructs your own shell to load and run that code, and it will actually move your current shell.

And if you run cd /etc/ first, then run that script, the new shell will already have /etc/ as its current directory, copying it from yours.

cd, umask, and variables in general are like that. If you run it in your shell, new shells created afterewards get copies of those settings, otherwise, each process is independent.

Init scripts like /etc/profile, et cetera, all run inside your current shell anyway.

Thanks for the quick reply! I'm still unclear on a few points and I hope you don't mind a follow up question to straighten me out

To address your first comment about "they do whatever their author wanted"... When inspecting the files in question I noticed the comment "Functions and aliases go in /etc/bashrc" in the /etc/profile file

Based on some of the posts I've read while researching the subject... I got the impression certain files had very specific purposes... And I've even seen posts were people were saying builtin commands (like umask) wouldn't work in certain files... Can you confirm this? If so I'm trying to understand the exact rules surrounding which sort of functionality the individual startup files can support

The second part of your comment I'd like to address is you said

"/etc/bashrc is not a file bash will load unless something else tells it to, but someone could easily have put /etc/bashrc into /etc/profile"

While trying to understand this topic I edited each file and put echo commands in the individual files to see when they'd start. The /etc/bashrc started in both login and non-login shells. It loaded after the /etc/profile in a login shell (putty) and in the non-login shell (The gnome GUI terminal) when the /etc/profile didn't run.

I looked through the profile and all the scripts under the /etc/profile.d directory and couldn't locate anything calling the /etc/bashrc script. Could I ask you for a clue on where else I might look to see what's starting it?

Thanks for your patience

The profile and login files are for setting your non-volatile environment. For instance, any environment variables, such as PATH, that you export. So these variables will stay with you from shell to subshell to subshell. In the olden days, when people logged in from a terminal, commands like stty would also appear in the login files, because no matter how many subshells you start, you won't change your terminal.

The basrc/kshrc and rc files for other shells are for the volatile environment, such as functions and aliases, which don't stay with you when you start a subshell, or variables that you don't for some reason export.

Before somebody corrects me, I should say that functions can be exported in bash (and I think ksh), but they don't tend to be.

Andrew

---------- Post updated at 09:19 AM ---------- Previous update was at 09:07 AM ----------

From the bash man page on my Ubuntu system:

       --init-file file
       --rcfile file
              Execute commands from file instead of the system  wide  initial
              ization file /etc/bash.bashrc and the standard personal initial
              ization file ~/.bashrc if the shell is interactive (see  INVOCA
              TION below).

If you have /etc/bashrc it is possible that your distribution has changed the name of the default system bashrc file (or maybe Ubuntu or Debian have). But this file is read by bash by default on startup.

Andrew

That's just someone arranging their own house as suits themselves, /etc/bashrc is something they invented. Maybe they wanted to put special BASH-only syntax in it, or maybe they worried about the system automatically replacing /etc/profile (which a few systems do).

I can confirm that it's what I described in my earlier post and absolutely nothing more. umask is not blocked.

The only "mystery" is when it's being run in the same shell, versus when you're running it in a different shell, and if you understand that ./scriptname creates a new process while . scriptname doesn't, you've already grasped the entire issue.

All profile files are executed in the current shell, by the way, they wouldn't be much use if they weren't.

Functionality is never limited (not unless you start asking it to limit things with funny flags like bash -r).

Are you sure? grep /etc/bashrc /etc/profile

Any other file these scripts read could be invoking it. If you could post the content of your ~/.bashrc and /etc/profile, we can suggest where to look next. Be sure to put them in code tags.

It's possible they customized BASH itself to use a different profile file, but I'm not sure why they'd bother... It's trivial to just dump a . /etc/bashrc into your ~/.bashrc