Glob pattern

One-liner program

A one-liner is a computer program or expression that takes no more than a single line.

In practice, one-liners will be entered and executed directly from the command-line. As a consequence, one-liners are usually implemented with languages like BASIC, Bourne shell, AWK, Python, or Perl. APL and later J are well known for their terseness and substantial libraries of one-liners. Some functional programming languages also permit one-liners.


The word One-liner 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.


The TAPL book contains 20 examples of One-liners (A Handful of Useful awk 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:

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

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

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.

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:

$ prog foo 'f??'; echo $?
$ prog 'best short program' '??st*o**p?*'; echo $?

One-liner in functional programming

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

main = (sequence . map putStrLn . List.sort . lines) =<< getContents 

An even shorter version.

 main = interact (unlines . List.sort . lines) 

Python is well suited for writing one liners using lambda functions without yielding obfuscated code. Here is a one liner in Python that multiplies two matrices (represented as a list of lists) z = lambda x, y : [[sum([x[i][k]*y[k][j] for k in range(len(x[0]))]) for j in range(len(y[0]))] for i in range(len(x))] Another, that prints all primes within the specified range [2, n] : z = lambda n : [x for x in range(2, n + 1) if len([i for i in range(2, x) if x%i == 0]) == 0] This performs a Discrete Time Convolution on two input lists and yields a new list z = lambda x, h : [sum((x[i - j] if i - j >= 0 and i - j < len(x) else 0)*h[j] for j in range(len(h))) for i in range(len(x) + len(h) - 1)]

External links

Search another word or see Glob patternon Dictionary | Thesaurus |Spanish
Copyright © 2015, LLC. All rights reserved.
  • Please Login or Sign Up to use the Recent Searches feature