Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-06-2005, 03:44 AM
|
#1
|
Member
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232
Rep:
|
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.
|
|
|
09-06-2005, 03:54 AM
|
#2
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,798
|
If using bash, try :
Code:
shopt | grep login_shell
|
|
|
09-06-2005, 04:09 AM
|
#3
|
Member
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232
Original Poster
Rep:
|
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?
|
|
|
09-06-2005, 04:20 AM
|
#4
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,798
|
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
|
|
|
09-06-2005, 04:42 AM
|
#5
|
Member
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232
Original Poster
Rep:
|
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?"
|
|
|
09-06-2005, 06:01 AM
|
#6
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,798
|
There are some ways, for example echo $TERM, if it is linux, then
you are surelly in a console terminal, so not in X
|
|
|
10-21-2015, 09:41 AM
|
#7
|
LQ Newbie
Registered: Jul 2015
Posts: 4
Rep: 
|
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.
|
|
|
10-21-2015, 10:09 AM
|
#8
|
LQ Guru
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 5,075
|
> 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 10:50 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|