One-liner program

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

Originally, a one-liner program was textual input to the command-line of an operating system shell that performs some function in just one line of input. The 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 scripting languages such as AWK, sed, and Perl have traditionally been adept at expressing one-liners. Specialist 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.


The concept of a one-liner program has been known since the 1960s[1] 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.[2]

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:

The 1977 version had only a few built-in variables and predefined functions. It was designed for writing short programs […] Our model was that an invocation would be one or two lines long, typed in and used immediately. Defaults were chosen to match this style […] We, being the authors, knew how the language was supposed to be used, and so we only wrote one-liners.

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.


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.


Simulated output of the 10PRINT one-liner BASIC program for the Commodore 64 — 10 PRINT CHR$(205.5+RND(1)); : GOTO 10

A single line of BASIC can typically hold up to 255 characters, and one liners ranged from simple games[3] 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


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 $?


The TAPL book contains 20 examples of one-liners at the end of the book's first chapter.

Here are the very first of them:

  1. Print the total number of input lines:
    END { print NR }
  2. Print the tenth input line:
    NR == 10
  3. Print the last field of every input line:
    { print $NF }


Here are examples in J:

  • A function avg to return the average of a list of numbers:
    avg=: +/ % #
  • Quicksort:
    quicksort=: (($:@(<#[) , (=#[) , $:@(>#[)) ({~ ?@#)) ^: (1<#)


Here are examples in the Perl programming language:

  • Look for duplicate words
perl -0777 -ne 'print "$.: doubled $_\n" while /\b(\w+)\b\s+\b\1\b/gi' 
  • Find Palindromes in /usr/dict/words
perl -lne 'print if $_ eq reverse' /usr/dict/words
  • in-place edit of *.c files changing all foo to bar
perl -p -i.bak -e 's/\bfoo\b/bar/g' *.c

Many one-liners are practical. For example, the following Perl one-liner will reverse all the bytes in a file:

perl -0777e 'print scalar reverse <>' filename

While most Perl one-liners are imperative, Perl's support for anonymous functions, closures, map, filter (grep) and fold (List::Util::reduce) allows the creation of 'functional' one-liners.

This one-liner creates a function that can be used to return a list of primes up to the value of the first parameter:

my $z = sub { grep { $a=$_; !grep { !($a % $_) } (2..$_-1)} (2..$_[0]) }

It can be used on the command line, like this:

perl -e'$,=",";print sub { grep { $a=$_; !grep { !($a % $_) } (2..$_-1)} (2..$_[0]) }->(shift)' number

to print out a comma-separated list of primes in the range 2 - number.


The following Haskell program is a one-liner: it sorts its input lines ASCIIbetically.

main = (mapM_ putStrLn . Data.List.sort . lines) =<< getContents -- In ghci a qualified name like Data.List.sort will work, although as a standalone executable you'd need to import Data.List.

An even shorter version:

main = interact (unlines . Data.List.sort . lines) -- Ditto.

Usable on the command line like:

cat filename | ghc -e "interact (unlines . Data.List.sort . lines)"


The following Racket program is equivalent to the above Haskell example:

#lang racket
(for-each displayln (sort (port->lines) string<?))

and this can be used on the command line as follows:

racket -e '(for-each displayln (sort (port->lines) string<?))'


Performing one-liners directly on the Unix command line can be accomplished by using Python's -cmd flag (-c for short), and typically requires the import of one or more modules. Statements are separated using ";" instead of newlines. For example, to print the last field of unix long listing:

ls -l | python -c "import sys;[sys.stdout.write(' '.join([line.split(' ')[-1]])) for line in sys.stdin]"

Python wrappers[edit]

Several open-source scripts have been developed to facilitate the construction of Python one-liners. Scripts such as pyp or Pyline import commonly used modules and provide more human-readable variables in an attempt to make Python functionality more accessible on the command line. Here is a redo of the above example (printing the last field of a unix long listing):

ls -l | pyp "whitespace[-1]" # "whitespace" represents each line split on white space in pyp
ls -l | pyline "words[-1]"  # "words" represents each line split on white space in pyline

Executable libraries[edit]

The Python CGIHTTPServer module for example is also an executable library that performs as a web server with CGI. To start the web server enter:

$ python -m CGIHTTPServer
Serving HTTP on port 8000 …

TCL Tool Control Language[edit]

There is a gold mine of One Liners Programs and content in the Tool Control language TCL 8.6 core distribution, TCL manual pages, and TCLLIB library that can be adapted or recast into brief one liners programs. These one liners programs or procedures can be pasted into the TCL 8.6 console window for quick results, reference the TCL Wiki. Some one liners programs use the return statement, return $value, or return 1 to return the results to the program line. Although many TCL programmers just rely on the last computation being returned by the one liner procedure in the TCL console window. There is some room in the Wiki publications for programming style differences. But it is usually best to put usage, credits, or TCL documentation references for the one liners procedures on separate comment lines. The random procedures make use of the random function and do not return the same answer every time. Dependence on math operator notation, helper procedures, math check examples, and special library functions should be noted in the comment lines.

There are pros and cons to one liner programs in TCL. One may contrast the approach to one liners programs in problem solving versus the traditional procedural approach. There are better routines and methods in faster language constructs in the current TCL core distribution and TCLLIB. Working with recursion, primes, text search, and timing the procedures will quickly show the warts on the one liners programs. To gain speed and shorter computation times, one will generally have to access the TCL core distribution and TCLLIB. Since the TCL interpreter collapses the carriage returns, skips, blank lines, and dead space of traditional written procedures into a single line of machine code, is not every script a one liner program to the parser? As grandfather remarked, the gourmet omelet, beef mulligan stew, and farm buttermilk all go to the same place.

    # compute the doubling time constant in years for money
    #  using interest rate in percent for exact constant 
    proc rule_72_constant {percent } {  expr { log( 2. )/ log (1. + $percent/100.)} }  
    # Usage rule_72_constant 10  returns 7.27253 years,
    # 7.27253  years to double money at 10 percent interest
    # setting the doubling constant at 10* 7.27253 percent rounds to 72.
    proc approximate_doubling_time {percent} { expr { 72. / $percent}}
    # example 1, { expr {72./10 }}   approximates 7.2 years
    # example 2, { expr {72./4 }}  approximates 18 years 
    # money at 4 percent doubles about 18 years
    # random integer in the range  zero to $nn 
    # one liner uses the expr calculation method
    proc  random_number_less_than nn { expr { int($nn * rand())}}
    # Usage  random_number_less_than 10
    # may return 3, 8, 9  or other random number
    # following one liners use math operator notation
    # degrees Centigrade to degrees  Fahrenheit 
    proc  Fahrenheit cc {  [+ [* 1.8 $cc]  32. ]}
    # Usage Fahrenheit 20 returns 68. degrees  Fahrenheit 
    # degrees Fahrenheit to degrees  Centigrade 
    proc Centigrade ff  { [/ [-  $ff 32. ] 1.8 ]}
    # Usage Centigrade 68 returns 20 degrees centigrade

Windows PowerShell[edit]

Finding palindromes in file words.txt

Get-Content words.txt | Where { $_ -eq -join $_[($_.length-1)..0] }

Piping semantics in PowerShell help enable complex scenarios with one-liner programs. This one-liner in PowerShell script takes a list of names and counts from a comma-separated value file, and returns the sum of the counts for each name.

ipcsv .\fruit.txt H F, C|Group F|%{@{"$($_.Name)"=($_.Group|measure C -sum).Sum}}|sort value

See also[edit]


  1. ^ 10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10 (PDF). Mit Press. 2014. p. 148. ISBN 9780262526746. Retrieved 3 July 2018.
  2. ^ "RUN magazine issue 35".
  3. ^ "Acorn User One-Line Games (Escape From Voros, Lexxias, Race To Varpon, Storm Clouds Over Zaqqit, Zander (AKA Lurch))". Retrieved 3 July 2018.

External links[edit]

  • Wikibooks Free Tcl Programming introduction & download pdf
  • [1] sourceforge net, download website and also Multiple computer languages
  • Tcl Sources, main Tcl and Tk source code download website
  • Tcler's Wiki, Tcl/Tk scripts and reference clearing house
  • TkDocs, Tcl/Tk Official documentation and archives