2

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?

Tom Hale
  • 40,825
  • 36
  • 187
  • 242

3 Answers3

7

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.

Tom Hale
  • 40,825
  • 36
  • 187
  • 242
  • I thought I had discovered a new shortest, as the [shortest quines list](http://codegolf.stackexchange.com/q/69/58256) currently says `Bash - 20 by LinusKrom`, but LinusKrom also gives this same 19 character solution [here](http://codegolf.stackexchange.com/a/96563/58256). – Tom Hale Dec 02 '16 at 13:34
0

I feel like:

cat $0

should work but they dont accept it

0

I think I just stumbled on a bash quine, which may be the shortest non-empty one, though cheating a little bit:

you write a file like this:

#!/bin/cat

do chmod a+x on the file

then do ./file

the output will produce the contents of the file itself lol

mrmtonio
  • 33
  • 5