Skip to content

Instantly share code, notes, and snippets.

@scvalex
Last active November 30, 2019 16:08
Show Gist options
  • Save scvalex/5360004 to your computer and use it in GitHub Desktop.
Save scvalex/5360004 to your computer and use it in GitHub Desktop.
A real quine in Bash
#!/bin/bash
T='#!/bin/bash
Q=$(printf "\x27") # A literal single quote
echo "$T" | head -n2 -
echo T=$Q"$T"$Q
echo "$T" | tail -n6 -'
Q=$(printf "\x27") # A literal single quote
echo "$T" | head -n2 -
echo T=$Q"$T"$Q
echo "$T" | tail -n6 -
@HaleTom
Copy link

HaleTom commented Dec 2, 2016

echo $BASH_COMMAND

:)

@kogorman
Copy link

kogorman commented Nov 30, 2019

Very good! I had to be very careful copying this -- copy and paste got it wrong (but created a different quine).

I preferred a slight change, to use the "-n+NUM" form of tail, to match the argument to head, and change the shebang to match my preferred usage, and wound up with:

#!/usr/bin/env bash
# As seen at https://gist.github.com/scvalex/5360004

T='#!/usr/bin/env bash
# As seen at https://gist.github.com/scvalex/5360004

Q=$(printf "\x27")  # A literal single quote

echo "$T" | head -n3 -
echo T=$Q"$T"$Q
echo "$T" | tail -n+3 -'

Q=$(printf "\x27")  # A literal single quote

echo "$T" | head -n3 -
echo T=$Q"$T"$Q
echo "$T" | tail -n+3 -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment