Write a self replicating program, in the source code filename H.*, contianed in an empty folder then manually compile and run or just run the program if your chosen language is dynamic, and it should then just create a new source file, e.*, with the same exact source code, and automatically compile/run that new program, which makes the new source file l.*, etc. until the source file !.* is made, and that program should do essentially nothing once it is compiled/run. "Hello world!" has some repeated characters, so the second character should be created by a similar filename, just with a new <space> at the end. Programs that just make/compile source files from the single instance of the program are invalid, and yes, " " is a valid filename. It should appear to say "Hello World!" when viewed in the file explorer sorted by time created.

For example, if you were to attempt this challenge in python, than you would create the source file H.py, which complies and runs e.py, which then itself creates l.py, that file l .py, o.py, .py, w.py, o .py, r.py, l .py and d.py which finally creates and runs !.py, which does not do anything once run.

  • \$\begingroup\$ "Self-replicating" implies that the program creates another file exactly identical to the original. Do the files all have to be identical, so that they have to read their own filename? \$\endgroup\$
    – Doorknob
    Commented May 24, 2015 at 22:31
  • \$\begingroup\$ maybe re-write this as an ouroboros challenge? instead of the programs compiling the next one, just output the source, so H.py produces e.py... d.py produces H.py \$\endgroup\$
    – Sparr
    Commented May 24, 2015 at 22:45
  • \$\begingroup\$ @Doorknob yes, They would have to have the same content, and need to check their filename to see what to make the next one called. \$\endgroup\$ Commented May 24, 2015 at 23:58
  • \$\begingroup\$ Shouldn't the example end with !.py? Also, it states that e.* should create e.* again. \$\endgroup\$ Commented May 25, 2015 at 0:51
  • 2
    \$\begingroup\$ There are 3 ls in Hello world!. What should the filename of the 3rd be? \$\endgroup\$
    – jimmy23013
    Commented May 25, 2015 at 1:31

2 Answers 2


Python 2, 194

import os,sys
h,A='H|e|l|l |o| |w|o |r|l  |d|!'.split('|'),sys.argv[0]
if a in d:o=d[a]+'.py';F(o,'w').write(F(A).read());os.popen('python "'+o+'"')
  • \$\begingroup\$ the code runs without an error, but doesn't appear to do anything? \$\endgroup\$ Commented May 25, 2015 at 8:57
  • \$\begingroup\$ oops, after re-reading the specs, it seems that I missed the fact that it had to run itself ... that's fixed \$\endgroup\$
    – dieter
    Commented May 25, 2015 at 9:05
  • \$\begingroup\$ you have to name the script "H.py" by the way \$\endgroup\$
    – dieter
    Commented May 25, 2015 at 9:08
  • \$\begingroup\$ It still seems to not be working, have you tried it yourself? \$\endgroup\$ Commented May 25, 2015 at 9:12
  • \$\begingroup\$ Yes I did ! I run under linux, so maybe running "python.exe" instead of "python" is the cause of the issue \$\endgroup\$
    – dieter
    Commented May 25, 2015 at 9:17

Bash, 99 88 84

X=`grep -Po "(?<=#$0#)[^#]*" "$0"` #H#e#l#l #o# #W#o #r#l  #d#!
cp "$0" "$X"
sh "$X"


  1. First we create a regex from the name of the file (the argument $0). For example, here is the regex constructed when the first file executes ($0 is H):


    This searches for a sequence of non-# characters preceded by #H#. That is, it finds the next filename in a sequence of #-separated filenames. (I used # so that the separator before H would function as the string delimiter itself, with the bonus of not needing a closing delimiter.)

  2. Then we search (grep) the currently-executing file "$0" with this regex. We need the -P flag (or similar) to use the lookbehind construct, and -o to only return the new filename, not the whole line.

  3. We store the result (using backticks `) of this whole evaluation in the variable X.

  4. We copy (cp) the current file to the new file.

  5. Finally we execute the new file with sh.

Example usage:

robert@unity:~/hwbash$ ls -lrt
total 4
-rw-r--r-- 1 robert robert 97 May 25 23:52 H
robert@unity:~/hwbash$ sh H
cp: cannot create regular file `': No such file or directory
sh: 0: Can't open
robert@unity:~/hwbash$ ls -lrt
total 48
-rw-r--r-- 1 robert robert 97 May 25 23:52 H
-rw-r--r-- 1 robert robert 97 May 25 23:52 e
-rw-r--r-- 1 robert robert 97 May 25 23:52 l
-rw-r--r-- 1 robert robert 97 May 25 23:52 l 
-rw-r--r-- 1 robert robert 97 May 25 23:52 o
-rw-r--r-- 1 robert robert 97 May 25 23:52  
-rw-r--r-- 1 robert robert 97 May 25 23:52 W
-rw-r--r-- 1 robert robert 97 May 25 23:52 o 
-rw-r--r-- 1 robert robert 97 May 25 23:52 r
-rw-r--r-- 1 robert robert 97 May 25 23:52 l  
-rw-r--r-- 1 robert robert 97 May 25 23:52 d
-rw-r--r-- 1 robert robert 97 May 25 23:52 !


  1. I saved some bytes by using bash H instead of #!/bin/bash. This also means that I don't have to copy the executable permission, so sorting by timestamps works now (but you need to add a sleep 1 or something to make it reliable).
  2. Saved some more bytes by moving the string to a comment and changing the command from bash to sh.
  • \$\begingroup\$ I would appreciate any bash masters who could advise me on where I can trim off whitespace, if any... \$\endgroup\$ Commented May 26, 2015 at 3:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.