ArticleslgStudy

computer science

One-liner program

One-liner program is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand One-liner program rather than just read about it. In short: 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 compil…

One-liner program — main illustration
One-liner program — illustration

Key takeaways

  • One-liner program belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect One-liner program to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of One-liner program from memory before moving on to harder problems.

Reference excerpt

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:

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.

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:

C The following example is a C program (a winning entry in the "Best one-liner" category of the IOCCC).

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:

AWK The book The AWK Programming Language contains 20 examples of 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 (like wc -l): Print the tenth input line: Print the last field of every input line:

J Here are examples in J:

A function avg to return the average of a list of numbers: Quicksort:

Perl 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:

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:

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.

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

An even shorter version:

Usable on the command line like:

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

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

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

Python 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 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):

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:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with One-liner program

Start with the simplest possible case. Write down what One-liner program claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to One-liner program before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about One-liner program ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of One-liner program

In research
One-liner program appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses One-liner program in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
One-liner program is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer programming, so understanding it makes those chapters shorter.
In everyday life
Look for One-liner program outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “One-liner program” →

How to study One-liner program in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what One-liner program means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain One-liner program out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is One-liner program in simple terms?

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 interpre…

Why does One-liner program matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study One-liner program?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on One-liner program.

Tags

  • Computer programming

Keep exploring