LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script non-interactive login shell (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-non-interactive-login-shell-4175536987/)

mick66 03-17-2015 05:17 AM

bash script non-interactive login shell
 
Hi

I'm focusing on getting my head around interactive v non-interactive shells, both non-login and login. Basically what scripts are run and how local and global variables behave.

I'm pretty much there except for 'non-interactive login shells'. I understand that 'non-interactive' shells are started when a script is run. However, if I start my script with #!/bin/bash --login, then a 'non-interactive login' shell will be started.

In my mind I would have expected this to be the same as logging in from scratch, albeit without a user\password prompt. In this case I would have thought that any exported variables from the parent shell would be blown away. This doesn't appear to be the case. The 'non-interactive login' shell appears to inherit the environment of its parent 'interactive' shell.

Can anyone explain the purpose of a 'non-interactive login' shell and how they behave in this regard.

Hope this makes sense.

Cheers

Mike

pan64 03-17-2015 06:23 AM

see man bash and look for the related files, initialization. Environment is always inherited, but every process can modify it ....

mick66 03-17-2015 06:41 AM

Hi

thanks for the reply but are you sure that's right ... environment is always inherited.

If I export a bunch of variables and start an interactive shell as in [mick@box1]> bash
then yes the variables are inherited.

If I do [mick@box1] > su tom then I've started an interactive shell under 'toms' login ... but not a login shell so 'micks' environmental variables are inherited.

If I do [mick@box1] > su - tom then I've started an interactive login shell and 'micks' environment isn't inherited.

So I guess environmental variables aren't always inherited.

My question was ... how do non-interactive login shells behave and why would you use one? As in bash scripts starting #!/bin/bash --login

Cheers

Mike

pan64 03-17-2015 06:44 AM

yes, please read the man page first. First the environment was inherited, next some parts were updated, overwritten or just dropped. Regarding env vars you can specify if you want to inherit that given var (those are the exported variables) or not at all.

mick66 03-17-2015 07:06 AM

Hi

to be honest ... I've read the man pages ... and I don't understand them fully. That's why I'm on a 'newbie forum'.

Cheers

Mick

pan64 03-17-2015 09:20 AM

so please post your questions, what is your problem exactly? first a shell started (inheriting the environment of its parent), next depending on its state (login and/or interactive) some initialization will occur.


All times are GMT -5. The time now is 01:41 AM.