bashquine

Shortest non-null bash quine


The shortest bash quine is the null string:

$ bash -c ''
$ 

What is the shortest non-null bash script which produces its own source code as output?


Solution

  • 19 characters:

    $echo 'echo $BASH_COMMAND' > quine
    $ wc -c quine
    19 quine
    $ cat quine
    echo $BASH_COMMAND
    $ bash quine
    echo $BASH_COMMAND
    $
    

    The character count includes the newline in the source (because it is produced in the output).


    If you've not heard of $BASH_COMMAND before, the manual says:

    BASH_COMMAND

    The command currently being executed or about to be executed, unless the shell is executing a command as the result of a trap, in which case it is the command executing at the time of the trap.