Linux - SoftwareThis 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
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.
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.
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 ?
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.
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.
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 ("-").
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.
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.
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.
*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.
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.
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. :/
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.