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: - kenneho |
$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 |
Quote:
|
Quote:
Now I just tired the following: Code:
anisha@linux:~> echo $-1 |
$- 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. |
Quote:
|
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.
|
Quote:
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 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.. |
Quote:
Code:
#include <unistd.h> |
All times are GMT -5. The time now is 05:09 PM. |