One-liner program


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
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.

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. 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$; : GOTO 10

C

The following example is a C program.

mainm

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

AWK

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
  2. Print the tenth input line: NR 10
  3. Print the last field of every input line:

    J

Here are examples in J:
Here are examples in the Perl programming language:
perl -0777 -ne 'print "$.: doubled $_\n" while /\b\b\s+\b\1\b/gi'
perl -lne 'print if $_ eq reverse' /usr/dict/words
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 and fold 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

It can be used on the command line, like this:
perl -e'$,=",";print sub ->' number
to print out a comma-separated list of primes in the range 2 - number.

Haskell

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

main = =<< 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 -- Ditto.

Usable on the command line like:

cat filename | ghc -e "interact "

Racket

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

  1. lang racket

and this can be used on the command line as follows:
racket -e ''

Python

Performing one-liners directly on the Unix command line can be accomplished by using Python's -cmd flag, 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;
"

Python wrappers

Several open-source scripts have been developed to facilitate the construction of Python one-liners. Scripts such as
or 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 :

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

Executable libraries

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 0.0.0.0 port 8000 …

Windows PowerShell

Finding palindromes in file words.txt

Get-Content words.txt | Where

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|%|sort value