LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What does $0 mean ? (https://www.linuxquestions.org/questions/linux-newbie-8/what-does-%240-mean-4175443752/)

.product 01-01-2013 11:02 PM

What does $0 mean ?
 
%> echo $0

-tcsh

It returns the current shell

Does it mean there exist variable named 0 ?

If yes how do you list all the existing variables, %> Set does not seem to list it.

Thanks,

shivaa 01-01-2013 11:46 PM

It contains the first word i.e. the command name, when use in a script.
Let's know that in what respect you're asking?

.product 01-01-2013 11:55 PM

Hi Shivaa,
I want to know what does it mean to the interpreter.
Is there a variable named 0, Available to the shell.


Thanks,
p.s. - I am new to the linux world and hope asking the right question.

shivaa 01-02-2013 12:26 AM

Hi, $0, $1, $2 etc are shell built-in variables, and called positional parameters.
For more clarification, refer: http://www.tldp.org/LDP/abs/html/oth...html#CHILDREF2

If you simply run it from shell i.e. echo $0, it will return the name of the running process i.e. shell. And if you use it within a script, then it will store the script name itself.

David the H. 01-02-2013 01:02 AM

You have to be careful about applying bourne-shell syntax explanations to the c-shells. There are many things about them that are very different.


In this case, however, it's correct. The "$0" positional stores the name of the command, in the form that it was run as (i.e. if you launch the script through a symlink, it will hold the link's name).

In bash, it's treated slightly differently from the rest of the parameters, in that it doesn't appear in the "$@/$*" lists, or get affected by commands like shift, but in all other respects it's just another parameter.

But parameter use in the c-shell appears to be a bit different, so it may not have quite the same behavior there. It seems, for example, that csh doesn't have a "$@" variable, and you have to use "$argv" instead.


There's a lot of documentation on the web about csh/tsch syntax. Just google it.

(And read here for several good reasons why you should avoid using it for scripting.)

http://www.grymoire.com/Unix/CshTop10.txt

jpollard 01-02-2013 09:12 AM

And note - the name provided is a name, this does not necessarily correspond to the file given.

As you saw, giving "echo $0" in your sell gave you the value "-tcsh". This isn't the command "tcsh". Login normally puts a "-" character in front of the users shell (as specified in /etc/passwd) file to indicate to the shell that it is an interactive shell. Under normal invocation, the parameter would have included "/bin/tcsh" (or "/usr/bin/tcsh") exactly as specified in the passwd file.

But to indicate to tcsh (and most shells) it tells the command interpreter that it should take special action (processing the login configuration scripts usually) the "-" is put on the beginning of the base file name.


All times are GMT -5. The time now is 03:55 PM.