ProgrammingThis 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.
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.
Depending on wether the shell is a login shell, a regular shell, or a screen utility, I get different results. Can anyone please give a brief explanation on this subject, or point me to relevant documentation? I've googled it, but haven't yet found any good info on this.
- kenneho
Click here to see the post LQ members have rated as the most helpful post in this thread.
$0 is the name of the running process. If you use it inside a shell, then it will return the name of the shell. If you use it inside a script, it will be the name of the script.
$0 is the name of the running process. If you use it inside a shell, then it will return the name of the shell. If you use it inside a script, it will be the name of the script.
Thanks for your reply. My examples are all bash processes, but how come the output are different, i.e. "-bash", "bash" and "/bin/bash"? I've read that login shell may differ from "regular" shells, but don't understand why. And what does "-" in from of a name (in this case "bash") mean?
$0 is the name of the running process. If you use it inside a shell, then it will return the name of the shell. If you use it inside a script, it will be the name of the script.
And $0 isn't really the name of the running process, at least I would not describe it that way. It's the name of the file as was invoked on the command line.
And $0 isn't really the name of the running process, at least I would not describe it that way. It's the name of the file as was invoked on the command line.
Many thanks for the informative post ! That was really helpful
Last edited by Aquarius_Girl; 01-18-2010 at 06:03 AM.
I first encountered special variables in Bourne shell programming, but the idea has been used a lot since. You'll see them in most any shell programming these days, and you'll find them in Perl and Ruby. Glad this helped.
My examples are all bash processes, but how come the output are different, i.e. "-bash", "bash" and "/bin/bash"? I've read that login shell may differ from "regular" shells, but don't understand why. And what does "-" in from of a name (in this case "bash") mean?
The value of $0 can be changed by the program/script itself. But by default the $0 of any process is the command that was used to start it.
So, if you have a bash script called test.sh in /usr/local/bin, and that directory is in your $PATH, you can start it by just typing the command: test.sh and $0 of the script will be "test.sh" by default.
But if /usr/local/bin is not in your path or if you just choose to start it with te command "/usr/local/bin/test.sh", then $0 will be "/usr/local/bin/test.sh".
Also, as mentioned, the command to start a process is just the default value for $0. A program or script can change its own $0 to something else. E.g, check what $0 is after doing this:
Code:
exec -abosh /bin/bash
So when bash is invoked as a login-shell, apperently it sets $0 to "-bash" to have an indication in the processlist that it is a login-shell process.
BTW $0 is also the name of the process as it wil be shown by programs like top, ps, etc..
And $0 isn't really the name of the running process, at least I would not describe it that way. It's the name of the file as was invoked on the command line.
I think it's argv[0], whatever that happens to be.
Code:
#include <unistd.h>
int main()
{
const char program[] = "/bin/sh";
const char *new_argv[2] = { "I AM /bin/sh!", NULL };
execvp(program, new_argv);
return 1;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.