One-liner program
Encyclopedia
A one-liner is 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 these Unix shell
s or the 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.
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.
Here are the very first of them:
Here are examples in J
:
Here are examples in Perl
:
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 language
s. 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, here split to two lines for presentation).
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 shell
s.
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 $?
program is a one-liner: it sorts its input lines asciibetically.
An even shorter version:
and this can be used on the command line as follows:
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:
It can be used on the command line, like this:
to print out a comma-separated list of primes in the range 2 - number.
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:
It returns with:
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:
External links
Shell (computing)
A shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel. However, the term is also applied very loosely to applications and may include any software that is "built around" a particular component, such as web...
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
Dynamic programming language
Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all...
scripting
Scripting language
A scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...
languages such as AWK, sed, and Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
have traditionally been adept at expressing one-liners.
Specialist shell
Shell (computing)
A shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel. However, the term is also applied very loosely to applications and may include any software that is "built around" a particular component, such as web...
interpreters such as these Unix shell
Unix shell
A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...
s or the Windows PowerShell
Windows PowerShell
Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on top of, and integrated with the .NET Framework...
, 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 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 UnixUnix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...
operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...
. The authors explain the birth of the One-liner paradigm with their daily work on early Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...
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
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
, which is a compiled language.
Examples
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:
- Print the total number of input lines:
END { print NR }
- Print the tenth input line:
NR 10
- Print the last field of every input line:
{ print $NF }
Here are examples in J
J (programming language)
The J programming language, developed in the early 1990s by Kenneth E. Iverson and Roger Hui, is a synthesis of APL and the FP and FL function-level languages created by John Backus....
:
- A function avg to return the average of a list of numbers:
avg=: +/ % #
- Quicksort:
quicksort=: (($:@(<#[) , (=#[) , $:@(>#[)) ({~ ?@#)) ^: (1<#)
Here are examples in Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
:
- Look for duplicate words
- Find Palindromes in /usr/dict/words
- in-place edit of *.c files changing all foo to bar
Many one-liners are practical. For example, the following Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
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 language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
s. 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
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
program (a winning entry in the "Best one-liner" category of the IOCCC, here split to two lines for presentation).
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 shell
Unix shell
A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...
s.
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 $?
Haskell
The following HaskellHaskell (programming language)
Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...
program is a one-liner: it sorts its input lines asciibetically.
An even shorter version:
Racket
The following Racket program is equivalent to the above Haskell example:and this can be used on the command line as follows:
Perl
While most PerlPerl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
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:
It can be used on the command line, like this:
to print out a comma-separated list of primes in the range 2 - number.
Python
Performing one liners directly on the Unix command line can be accomplished by using python's -cmd flag (-c for short), and typically requiresthe import of one or more modules. Statements are separated using ";" instead of newlines. For example, to print the last field of unix long listing:
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
It returns with:
Serving HTTP on 0.0.0.0 port 8000 ...
Python wrappers
Several open-source scripts have been developed to facilitate the construction of python one liners. Scripts such aspyp 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:
External links
- awk one-liners
- sed one-liners
- Awk and Sed One-Liners Explained
- Perl One Liners, practical examples
- More Perl One Liners
- Oneliner Contest 2007, the longest thread ever about one liner development for Sinclair machines
- Oneliner Contest 2008, ongoing thread about one liner development for Sinclair machines
- International Obfuscated C Code Contest, IOCCC
- Lambda One Liners in Python
- Pyline:a grep-like, sed-like command-line tool (Python recipe)
- pyp: Python Power at the Prompt