Kolam Ayer MakersLinux Foundations

One-Liners

Core Idea

A one-liner is a small shell program typed at the prompt instead of saved in a script file.

It can combine commands, pipes, redirection, variables, command substitution, loops, and conditionals. This is where separate Linux skills start becoming one tool.

Build It In Layers

Do not start with a clever compressed command. Build the pieces first.

history
history | grep ssh
history | grep ssh | wc -l

Then keep the final one-liner only if you can still explain every stage.

Common Forms

Pipeline:

history | grep ssh | wc -l

Run the next command only if the first succeeds:

mkdir -p ~/playground/oneliners && cd ~/playground/oneliners

Group several commands and redirect their combined output:

{ printf '# Status\n\n'; date; hostname; } > ~/src/pages/status.md

Loop over a list:

for path in ~/src/pages/*.md; do printf '%s\n' "$path"; done

Branch on a command result:

if curl -fsS https://example.org >/dev/null; then printf 'up\n'; else printf 'down\n'; fi

Use command output as an argument:

printf 'host=%s date=%s\n' "$(hostname)" "$(date +%F)"

When To Stop Being Clever

Move the one-liner into a script when it is reused, risky, hard to quote, hard to explain, or longer than one screen line.

A good one-liner is compressed, not mysterious. The skill is composition, not showing off.

Safety Rules

Proof Check

Write one one-liner that creates a Markdown file containing a heading, the current date, and the hostname. Then rewrite it as a multi-line script. Explain why both versions do the same work.

Docs Pointers