One-liner program

Uses your system voice; voice and rate in settings.

Snapshot

≈ 5 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

In computer programming, a one-liner program originally was textual input to the command line of an operating system shell that performed some function in just one line of input. In the present day, a one-liner can be * an expression written in the language of the shell; * the invocation of an interpreter together with program source for the interpreter to run; * the invocation of a compiler together with source to compile and instructions for executing the compiled program. Certain dynamic languages for scripting, such as AWK, sed, and Perl, have traditionally been adept at expressing one-liners. Shell interpreters such as Unix shells or Windows PowerShell allow for the construction of powerful one-liners. The use of the phrase one-liner has been widened to also include program-source for any language that does something useful in one line.

In computer programming, a one-liner program originally was textual input to the command line of an operating system shell that performed some function in just one line of input. In the present day, a one-liner can be an expression written in the language of the shell; the invocation of an interpreter together with program source for the interpreter to run; the invocation of a compiler together with source to compile and instructions for executing the compiled program. Certain dynamic languages for scripting, such as AWK, sed, and Perl, have traditionally been adept at expressing one-liners. Shell interpreters such as Unix shells or Windows PowerShell allow for the construction of powerful one-liners. The use of the phrase one-liner has been widened to also include program-source for any language that does something useful in one line.

History

The concept of a one-liner program has been known since the 1960s with the release of the APL programming language. With its terse syntax and powerful mathematical operators, APL allowed useful programs to be represented in a few symbols. In the 1970s, one-liners became associated with the rise of the home computer and BASIC. Computer magazines published type-in programs in many dialects of BASIC. Some magazines devoted regular columns solely to impressive short and one-line programs. The word One-liner also has two references in the index of the book The AWK Programming Language (the book is often referred to by the abbreviation TAPL). It explains the programming language AWK, which is part of the Unix operating system. The authors explain the birth of the one-liner paradigm with their daily work on early Unix machines: Notice that this original definition of a one-liner implies immediate execution of the program without any compilation. So, in a strict sense, only source code for interpreted languages qualifies as a one-liner. But this strict understanding of a one-liner was broadened in 1985 when the IOCCC introduced the category of Best One Liner for C, which is a compiled language.

Examples

One-liners are also used to show off the differential expressive power of programming languages. Frequently, one-liners are used to demonstrate programming ability. Contests are often held to see who can create the most exceptional one-liner.

BASIC

A single line of BASIC can typically hold up to 255 characters, and one liners ranged from simple games to graphical demos. One of the better-known demo one-liners is colloquially known as 10PRINT, written for the Commodore 64: 10 PRINT CHR$(205.5+RND(1)); : GOTO 10

C

The following example is a C program (a winning entry in the "Best one-liner" category of the IOCCC). main(int c,char**v){return!m(v[1],v[2]);}m(char*s,char*t){return*t-42?*s?63

*t|*s

  • t&&m(s+1,t+1):!*t:m(s,t+1)||*s&&m(s+1,t);} This one-liner program is a glob pattern matcher. It understands the glob characters , meaning zero or more characters, and , meaning exactly one character, just like most Unix shells. Run it with two args, the string and the glob pattern. The exit status is 0 (shell true) when the pattern matches, 1 otherwise. The glob pattern must match the whole string, so you may want to use * at the beginning and end of the pattern if you are looking for something in the middle. Examples: $ ./a.out foo 'f??'; echo $? $ ./a.out 'best short program' '??st*o**p?*'; echo $?

Text derived from https://en.wikipedia.org/w/index.php?oldid=1377474995 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

3 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.