Skip to content

Instantly share code, notes, and snippets.

@jeremyBanks
Created June 22, 2010 05:10
  • Star 18 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jeremyBanks/448040 to your computer and use it in GitHub Desktop.
one-line self-executing c header and expanded explanation
//

Since we don't want this visible in C, we put it in a comment.

&>/dev/null

Unfortunately // is interpreted as an invalid shell command and produces an error message, so we need to redirect that to /dev/null to get rid of it.

;x="${0%.*}"

This determines what our compiled filename will be by using string replacement to get rid of any extensions on the current filename.

;[ ! "$x" -ot "$0" ]||

This check if there already is a compiled version newer than the source file. If not...

(rm -f "$x";cc -o "$x" "$0")

...we delete any existing version and then compile our code, using our chosen compiled filename.

&&exec "$x" "$@"

If the compile was successful or unnecessary, pass execution to the compiled code, passing on the arguments given to this script.

//&>/dev/null;x="${0%.*}";[ ! "$x" -ot "$0" ]||(rm -f "$x";cc -o "$x" "$0")&&exec "$x" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment