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. |
If using bash, try :
Code:
shopt | grep login_shell |
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?
|
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 |
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?" |
There are some ways, for example echo $TERM, if it is linux, then
you are surelly in a console terminal, so not in X |
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. |
> 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. |