Fun with polymorphic shell scripts

Imagine you have two shell scripts in the same directory.


echo "I am test1"


echo "I am test2"

echo "This should not get printed"

Now run What do you expect the output to be?

I’d imagine that bash would run a copy of in memory, and as a result of that execution, would be replaced with at the end of execution. But I would be wrong. This is the output I get after running

$ ./ 
I am test1
This should not get printed

What the?


Evan Solomon

I actually made a demo in one liner form while you were writing this:

git clone shells-are-crazy && cd shells-are-crazy && chmod +x *.sh && ./ | grep "should not get printed"
Kyle Rankin

That is completely counterintuitive. I had assumed it runs a copy in memory (and it behaves like it does this to a point):


echo "I am test3"

echo "Should this get printed? I don't know anymore"
$ ./ 
I am test3
  File: `'
  Size: 125       	Blocks: 24         IO Block: 4096   regular file
Device: 14h/20d	Inode: 6558960     Links: 1
Access: (0775/-rwxrwxr-x)  Uid: ( 1000/    kyle)   Gid: ( 1000/    kyle)
Access: 2013-03-01 17:48:22.337170925 -0800
Modify: 2013-03-01 17:48:13.661170924 -0800
Change: 2013-03-01 17:48:19.833171074 -0800
 Birth: -
stat: cannot stat `': No such file or directory
Should this get printed? I don't know anymore

Care to Comment?

Or if you'd prefer to get in touch privately, please send me an email.


Email (optional)

Blog (optional)