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.
These are the files bash loads and when it loads them. Anything else is a command someone dropped into one of those files.
Quote:
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
Imagine this script:
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.
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.
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?
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 ----------
Quote:
Originally Posted by bodisha
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
From the bash man page on my Ubuntu system:
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.
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
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).
Quote:
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?
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.
Quote:
If so I'm trying to understand the exact rules surrounding which sort of functionality the individual startup files can support
Functionality is never limited (not unless you start asking it to limit things with funny flags like bash -r).
Quote:
"/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.
Are you sure? grep /etc/bashrc /etc/profile
Quote:
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?
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
Last edited by Corona688; 11-09-2017 at 12:04 PM..
I am not able to understand below line in unix bash shell.Could anyone explain what it will do
result="${path1}/*${var1}*${var2}*wssreligibleitem*.csv"
path1 is defined and it is a directory path
var1 is defined and it holds string value like abc
var2 is defined and it holds string value like... (6 Replies)
Hi
There is an application installed on a server, that has a unique login account, but many users are using it with the same login name! How can we overcame this by creating individual accounts for the same application login account? (11 Replies)
Hi All,
I need some urgent help regarding some info.
I have a cluster of servers for which I have two scripts for management.
control.sh is a bash script meant for restarting/stopping the servers.
manger.ksh is a kshell script. It is a master script to manage restarting/stoppping and... (3 Replies)
Hi all,
I am new to linux routing and would like to keep a possible running dialog about some scripts I have been studying and what the different parts of them mean. We are using Openwrt backfire along with openvpn and Swyx as VoIP. My goal is to eventually implement some QoS using dsmark, but... (1 Reply)
Hi,
I have to write a program to compute the checksums of files
./script.sh
I wrote the program using bash and it took me forever since I am a beginner but it works very well.
I'm getting so close to the deadline and I realised today that actually I have to use normal Bourne shell... (3 Replies)
What is the difference between calling the sub scripts of below two line.
/home/scripts/devdb.sh
. /home/scripts/devdb.sh
sh /home/scripts/devdb.sh
We are using the suse 2.0 version (4 Replies)
Hello Friends,
I am new to the scripting & have to analyze bunch of regular production scripts. It has .ksh which calls on the .awk script having many functions
I need to understand and debug the scripts ASAP
Can anybody please let me know as how can I debug, I want to see the flow of code... (3 Replies)
I'm trying to get a clear picture of how startup scripts are executed during
bootup. When run-level N is entered, the scripts in /rcN.d are executed.
I understand that the S* scripts are executed in numerical order during bootup.
What I don't understand is if the K* scripts are executed... (0 Replies)