LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-18-2010, 01:52 AM   #1
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Rep: Reputation: 40
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
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-18-2010, 02:19 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
$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
 
2 members found this post helpful.
Old 01-18-2010, 02:29 AM   #3
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by David the H. View Post
$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?
 
Old 01-18-2010, 03:26 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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 ?
 
Old 01-18-2010, 05:51 AM   #5
vrmartin2
Member
 
Registered: Dec 2009
Location: NE Ohio
Distribution: Open SUSE
Posts: 43

Rep: Reputation: 19
$- 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.
 
2 members found this post helpful.
Old 01-18-2010, 06:00 AM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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

Last edited by Aquarius_Girl; 01-18-2010 at 06:03 AM.
 
Old 01-18-2010, 06:27 AM   #7
vrmartin2
Member
 
Registered: Dec 2009
Location: NE Ohio
Distribution: Open SUSE
Posts: 43

Rep: Reputation: 19
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.
 
Old 01-18-2010, 06:48 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by kenneho View Post
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..

Last edited by Hko; 01-18-2010 at 06:50 AM.
 
Old 01-19-2010, 01:21 AM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by vrmartin2 View Post
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
 
1 members found this post helpful.
  


Reply

Tags
echo



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
"echo hello > /dev/ttyS0" does not return. rohitkugve Programming 2 01-11-2010 12:48 PM
Problem "$value=`mpstat 1 1 | grep "Average"`;" Alias pipe return nothing adamlucansky Linux - General 8 09-25-2009 07:26 AM
What does echo alias "char-major-10-224 off" >> /etc/modules.conf do? abefroman Linux - Software 2 09-16-2009 10:10 PM
echo mypage.htm | mutt -s "hello news" myemail@gmail.com frenchn00b Linux - General 16 04-20-2009 01:21 AM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 02:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:07 PM.

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