LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   What exactly does "echo $0" return? (https://www.linuxquestions.org/questions/programming-9/what-exactly-does-echo-%240-return-782908/)

kenneho 01-18-2010 01:52 AM

What exactly does "echo $0" return?
 
Hi.


I'm confused about what "echo $0" in a shell actually return. Consider these tests:
Code:

[root@e11apvl151 ~]# # Login shell:
[root@e11apvl151 ~]# echo $0
-bash
[root@e11apvl151 ~]# # Starting a new shell
[root@e11apvl151 ~]# bash
[root@e11apvl151 ~]# echo $0
bash
[root@e11apvl151 ~]# screen
[root@e11apvl151 ~]# echo $0
/bin/bash

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

David the H. 01-18-2010 02:19 AM

$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.

http://www.tldp.org/LDP/abs/html/oth...html#CHILDREF2

kenneho 01-18-2010 02:29 AM

Quote:

Originally Posted by David the H. (Post 3830549)
$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.

http://www.tldp.org/LDP/abs/html/oth...html#CHILDREF2

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?

Aquarius_Girl 01-18-2010 03:26 AM

Quote:

Originally Posted by David the H.
$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.

Well that was enlightening :)

Now I just tired the following:
Code:

anisha@linux:~> echo $-1
himBH1
anisha@linux:~> echo $-2
himBH2
anisha@linux:~> echo $-3
himBH3

Can you throw some light on the above shown output ?

vrmartin2 01-18-2010 05:51 AM

$- is a special variable as is $0. So, you're echoing $- plus a digit. See:

http://www.mcsr.olemiss.edu/unixhelp...crpt2.2.2.html

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.

Aquarius_Girl 01-18-2010 06:00 AM

Quote:

Originally Posted by vrmartin2
$- is a special variable as is $0. So, you're echoing $- plus a digit. See:

http://www.mcsr.olemiss.edu/unixhelp...crpt2.2.2.html

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 :)

vrmartin2 01-18-2010 06:27 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.

Hko 01-18-2010 06:48 AM

Quote:

Originally Posted by kenneho (Post 3830556)
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..

Simple, no magic at all..

ta0kira 01-19-2010 01:21 AM

Quote:

Originally Posted by vrmartin2 (Post 3830686)
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;
}

Kevin Barry


All times are GMT -5. The time now is 05:09 PM.