Understanding the difference between individual BASH login scripts

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Understanding the difference between individual BASH login scripts
# 1  
Old 11-08-2017
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!
# 2  
Old 11-08-2017
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.

Code:
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.

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:

Code:
#!/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.
# 3  
Old 11-08-2017
Quote:
Originally Posted by Corona688
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?

Thanks for your patience
# 4  
Old 11-09-2017
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:
Code:
       --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
# 5  
Old 11-09-2017
Quote:
Originally Posted by bodisha
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Understanding bash code

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)
Discussion started by: vamsi.valiveti
6 Replies

2. Solaris

Individual usernames for the same login account

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)
Discussion started by: fretagi
11 Replies

3. Shell Programming and Scripting

Difference between kshell and bash shell scripts Example cited

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)
Discussion started by: ankur328
3 Replies

4. IP Networking

Help understanding iproute2 and tc scripts

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)
Discussion started by: shodg001
1 Replies

5. Shell Programming and Scripting

Need a better understanding of shell scripts

Need a better understanding of shell scripts (14 Replies)
Discussion started by: sureshkumar4737
14 Replies

6. Shell Programming and Scripting

Changing the Bash Scripts to Bourne Scripts:URGENT

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)
Discussion started by: pgarg1989
3 Replies

7. Shell Programming and Scripting

Difference between calling the sub scripts

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)
Discussion started by: kingganesh04
4 Replies

8. Shell Programming and Scripting

Need help on understanding the Shell and AWK scripts

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)
Discussion started by: amberj123
3 Replies

9. UNIX for Dummies Questions & Answers

Understanding System Vish startup scripts

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)
Discussion started by: darkmatter14B
0 Replies

10. UNIX for Dummies Questions & Answers

Difference in Shell Scripts

Hi, Is anyone can help me to find out the difference in Shell Scripts between HP and Sun. Thanks in advance, Vijay R (3 Replies)
Discussion started by: rv_kumar
3 Replies
Login or Register to Ask a Question