The Strange Behavior of $0

Prateek Chandra
3 min readJun 24, 2020

Hello Everyone, Welcome to the blog for the action of $0 command in Terminal. While giving some CTF’s (Capture The Flag) which are a part of cybersecurity, I found this amazing thing for $0.

Let’s get started.

First, Let’s see about echo $0. Type echo $0 in our terminal

In the meantime, we found that it just returns the name of the shell that is bash but that’s not true for the case the same command is given in a file called zero.sh and after executing it,

From the above examples we conclude that echo $0 return the name for the file.

So we have seen the characteristics of echo $0. Now see something more interesting let’s type ls and $0 with an operand pipe(‘|’)

We can see some weird text “bash: line — : — — command not found” basically this is the main task of $0 it with ls it lists the directories and after that, it tries to execute it as if for example if the “Desktop” would be a command then it would execute it but it is not a command. On the other hand, we see the same about dnsrecon (it is a Cyberforensic tool) that required argument and just the details about it just normal as we type dnsrecon on the terminal without arguments.

Let’s see the same thing with the cat command.

We can see that it creates just a empty shell for us just like a shell as sh. Let’s type some commands on this.

Whoaa. It is just a NEW SHELL behaving like bash or sh, It is the main thing I found in CTF’s that we get to know more about a particular command.

Also tries same ls and $0 with an operand ‘&’, you find much interesting thing in our first line (that’s our process id :))

I Hope, you like it and tried this $0 command in your terminal. Thanks

Suggestions are welcome for this blog.

--

--

Prateek Chandra

CyberSecurity|Software Devloper|Love To explore Oceans|Marine Archeaology Enthusiast | IIT Roorkee