1870
Comment: add the solution that i came up with when someone asked about quines 4 days ago
|
2612
use `--' instead of `-' in shebang
|
Deletions are marked like this. | Additions are marked like this. |
Line 37: | Line 37: |
This other one consists of a function that uses `declare -f` to print its own definition and the remaining code. | This one consists of a function that uses `declare -f` to print its own definition. |
Line 39: | Line 39: |
The function's code needs to be written in this precise format and there needs to be a trailing space after `f ()` and `{` to match `declare -f`'s output. | NB: there is an extra space after `f ()` and after `{` to match the output of `declare -f`. |
Line 41: | Line 42: |
#!/bin/bash - | #!/bin/bash -- |
Line 44: | Line 45: |
echo '#!/bin/bash -'; | printf '#!/bin/bash --\n'; |
Line 46: | Line 47: |
echo f | printf 'f\n' |
Line 51: | Line 52: |
(May add more later if I figure them out.) | Here's a variant using a similar technique: {{{#!highlight bash f () { printf "%s\n${!1} $1" "$(local -f ${!1})" } f FUNCNAME }}} '''Notes''': this one also has an extra space after the first two lines, ''and'' it must have no trailing newline at the end of the file. And along the same lines - a `DEBUG` trap that prints its own definition: {{{#!highlight bash trap -- 'printf "%s\n:" "$(trap -p DEBUG)"' DEBUG : }}} This one uses `eval` together with an "assign default value" parameter expansion. It works from an interactive bash or ksh shell, and assumes (in fact, requires) that the variable `s` is not already defined: {{{ q=\' b=\\ eval ${s='echo q=$b$q b=$b$b eval \${s=$q$s$q}'} }}} It also relies on a particular implementation of `echo`, which differs between dash and bash. It doesn't work with dash's `echo`. |
Bash quine
A quine is a program that takes no input, and prints its own source code as output. Writing one is challenging in any language, and usually involves poking into the darkest, smelliest corners of the syntax.
If you've come across this page through some sort of wandering browsing, you might wish to stop reading after this introduction, and attempt to write a quine yourself. Reading the finished results here is not as educational as the struggle to write one. If on the other hand you've already struggled, either successfully or unsuccessfully, and would like to compare your results against someone else's, then read on.
Invalid quines
The classic shell approach would be something like
However, this is considered invalid, as it takes input from the file system.
The other classic shell approach would be the empty program. This one is considered to fail the spirit of the challenge, because it doesn't teach you much.
Valid quines
This first one is derived from the Java quine posted on the wikipedia page.
This one consists of a function that uses declare -f to print its own definition.
NB: there is an extra space after f () and after { to match the output of declare -f.
Here's a variant using a similar technique:
Notes: this one also has an extra space after the first two lines, and it must have no trailing newline at the end of the file.
And along the same lines - a DEBUG trap that prints its own definition:
This one uses eval together with an "assign default value" parameter expansion. It works from an interactive bash or ksh shell, and assumes (in fact, requires) that the variable s is not already defined:
q=\' b=\\ eval ${s='echo q=$b$q b=$b$b eval \${s=$q$s$q}'}
It also relies on a particular implementation of echo, which differs between dash and bash. It doesn't work with dash's echo.