LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-05-2015, 06:33 PM   #1
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Rep: Reputation: 17
Knowing that bash is logging in within .bashrc


Typically the user's .bash_profile script has logic in it to invoke the user's .bashrc if it exists. The .bashrc script is also invoked each time a user opens a terminal. What I want to do is to have the logic of my .bashrc file be different between when it is initially called from .bash_profile and otherwise. Is there something I can test in my .bashrc in order to know that bash is being invoked at log-in time or not ?
 
Old 10-06-2015, 05:26 AM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Something like this may work:

Code:
if [[ $(shopt -q login_shell) ]]
then
    ...
fi
Though I do suppose there's a reason that the distro's default profile doesn't check it to begin with.

Last edited by goumba; 10-06-2015 at 05:40 AM. Reason: Spelling and other idiotic stuff.
 
Old 10-06-2015, 05:27 AM   #3
SteveG_0001
LQ Newbie
 
Registered: Mar 2015
Posts: 17

Rep: Reputation: Disabled
Try somethung like
who|grep `id -un`|wc -l
and test result for greater than 1. It just counts how many times your logged in. Might not work if gui does not maintain utmp.
 
Old 10-06-2015, 11:40 PM   #4
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by goumba View Post
Something like this may work:

Code:
if [[ $(shopt -q login_shell) ]]
then
    ...
fi
Though I do suppose there's a reason that the distro's default profile doesn't check it to begin with.

Looks like the right way to do it. Thanks !
 
Old 10-07-2015, 01:27 AM   #5
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by goumba View Post
Something like this may work:

Code:
if [[ $(shopt -q login_shell) ]]
then
    ...
fi
Though I do suppose there's a reason that the distro's default profile doesn't check it to begin with.
Unfortunately it does not work. The '$(shopt -q login_shell)' path never gets invoked, even when called from .bash_profile.
 
Old 10-07-2015, 02:24 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,250

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
How do you know that? (try without -q)
 
Old 10-07-2015, 07:21 AM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,815

Rep: Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238
Quote:
Originally Posted by goumba View Post
Something like this may work:

Code:
if [[ $(shopt -q login_shell) ]]
then
    ...
fi
What that is doing is running shopt with the "-q" option, which suppresses the output, and then testing whether that output is non-null. That will never be true.

This should work:
Code:
if shopt -q login_shell
then
    ...
fi
 
Old 10-07-2015, 08:26 AM   #8
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Quote:
Originally Posted by rknichols View Post
What that is doing is running shopt with the "-q" option, which suppresses the output, and then testing whether that output is non-null. That will never be true.
[/code]
That's true. We wanted to test the return code. My mistake, doing things rushed again. My apologies to the OP.

In penance, I also offer than the OP may test the first character of argument 0 ($0) for a hyphen ("-").

*slinks under a rock*

Last edited by goumba; 10-07-2015 at 08:32 AM.
 
Old 10-07-2015, 03:52 PM   #9
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by rknichols View Post
What that is doing is running shopt with the "-q" option, which suppresses the output, and then testing whether that output is non-null. That will never be true.

This should work:
Code:
if shopt -q login_shell
then
    ...
fi
That does work. Thank you !
 
Old 10-07-2015, 03:55 PM   #10
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by goumba View Post
That's true. We wanted to test the return code. My mistake, doing things rushed again. My apologies to the OP.

In penance, I also offer than the OP may test the first character of argument 0 ($0) for a hyphen ("-").

*slinks under a rock*
Apology accepted <g>. Isn't argument 0 the process name ? What does starting with a hyphen mean ?
 
Old 10-07-2015, 07:44 PM   #11
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Quote:
Originally Posted by eldiener View Post
Apology accepted <g>. Isn't argument 0 the process name ? What does starting with a hyphen mean ?
In this case, $0 is the name of the shell, if invoked via other than -c, which will be some form of bash. I have gotten both bash and /bin/bash on the same system, the former in a text console, the latter in a terminal under X. However, the following is always true, from the bash man page:

Code:
INVOCATION
       A login shell is one whose first character of argument zero is a -,  or
       one started with the --login option.
So, in a login shell, echo $0 will return "-bash"; otherwise, simply "bash".

Last edited by goumba; 10-07-2015 at 07:54 PM. Reason: Removed extranneous info.
 
Old 10-08-2015, 01:41 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,250

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by goumba View Post
So, in a login shell, echo $0 will return "-bash"; otherwise, simply "bash".
No, unfortunately it is not so simple, just try to execute/source a shell script ($0 will be the name of the script itself).
But $0 will be equal to -bash in case of a login shell.
 
Old 10-08-2015, 07:45 AM   #13
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
*sgh* Alas, you are right, as like a dummy, I did my testing with bashrc and with interactive shells. The hyphen being the first character is right, however.

Last edited by goumba; 10-08-2015 at 11:59 AM.
 
Old 10-08-2015, 09:16 AM   #14
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,815

Rep: Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238
But, the OP was talking about doing this in .bashrc, which is sourced by the running shell. $0 and the login_shell option would have their values for that shell. Now you're talking about doing this in some other shell script, which would be interpreted by a different shell instance, not the login shell.
 
Old 10-08-2015, 11:59 AM   #15
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Quote:
Originally Posted by rknichols View Post
But, the OP was talking about doing this in .bashrc, which is sourced by the running shell. $0 and the login_shell option would have their values for that shell. Now you're talking about doing this in some other shell script, which would be interpreted by a different shell instance, not the login shell.
I'm not sure if you're referring to my post, as yours is right under mine, but I made no mention of other scripts. :/
 
  


Reply

Tags
bash, login, terminal


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Tinkered with some bash.bashrc files and the Terminal is now permamently in Bash youbunto0 Linux - Newbie 2 10-03-2014 11:27 AM
[SOLVED] Fresh install, user's .bashrc isn't getting run when logging in from SSH maples Debian 2 07-13-2014 06:17 PM
~/.bashrc, /etc/bash.bashrc files not read? Tachtory Slackware 3 01-04-2014 12:25 AM
How to change .bashrc prompt for logging in via SSH. Amckin Linux - Laptop and Netbook 1 11-04-2009 12:48 PM
Logging in Error in Fedora 5, /etc/bashrc command not found EmmaTheViking Linux - Newbie 10 05-24-2006 11:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration