LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to check in a script whether the shell is login or non login? (https://www.linuxquestions.org/questions/programming-9/how-to-check-in-a-script-whether-the-shell-is-login-or-non-login-360629/)

frankie_DJ 09-06-2005 03:44 AM

How to check in a script whether the shell is login or non login?
 
I am sure this has been asked before, but since I can't find it and since I am sure the answer is quite short....I'll ask again.

Thanks.

keefaz 09-06-2005 03:54 AM

If using bash, try :
Code:

shopt | grep login_shell

frankie_DJ 09-06-2005 04:09 AM

Thanks, I didn't know about shopt...but, now I keep opening xterms (Konsoles to be accurate) and when I 'shopt', in every terminal I open the login_shell toggles are switched to ON. That can't be right. Can it?

keefaz 09-06-2005 04:20 AM

Your shell in konsole is a login shell, but if you start a bash script
without the -l option, it won't be a login shell

try :
Code:

#!/bin/bash
echo $(shopt | grep login_shell)

Then execute the script in your konsole terminal

frankie_DJ 09-06-2005 04:42 AM

I see what you are saying. Only if it's a child process it is not a login shell. Well, that certainly doesn't solve my problem.

I guess then I should ask my question as: "How can I check in a script whether the current shell is the one in which I actually logged in, gave my uname and password or any other shell that I opened by starting Xserver and clicking on the xterm icon?"

keefaz 09-06-2005 06:01 AM

There are some ways, for example echo $TERM, if it is linux, then
you are surelly in a console terminal, so not in X

Quantim 10-21-2015 09:41 AM

To tell if you are in a login shell:

prompt> echo $0
-bash # "-" is the first character. Therefore, this is a login shell.

prompt> echo $0
bash # "-" is NOT the first character. This is NOT a login shell.

Information can be found in `man bash` (search for Invocation). Here is an excerpt:

A login shell is one whose first character of argument zero is a -, or
one started with the --login option.

You can test this yourself. Anytime you SSH, you are using a login shell. For Example:

prompt> ssh user@localhost
fervor@localhost's password:
prompt> echo $0
-bash

The importance of using a login shell is the any settings in `/home/user/.bash_profile` will get executed. Here is a little more information if you are interested (from `man bash`)

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and
executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile,
~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
The --noprofile option may be used when the shell is started to inhibit this behavior.

NevemTeve 10-21-2015 10:09 AM

> How can I check in a script whether the current shell is the one in which I actually logged in, gave my uname and password or any other shell that I opened by starting Xserver and clicking on the xterm icon?

untested:
Code:

if ps h $PPID | grep -q login; then echo login; fi


All times are GMT -5. The time now is 06:07 PM.