seq $1|sed G\;h
Try it online!
Outputs like so, 1\n\n2\n1\n\n3\n2\n1\n\n[...]
seq $1
creates a sequence from 1 to the first argument $1
|sed ...
which is piped into a sed script
sed works on a line-by-line basis; it first reads the first line into the buffer, called the "pattern space", after which the program commands is run on it. At the end of the program's execution on the first line, the remaining pattern space is implicitly printed. Then sed reads the next line into the pattern space, replacing the previous contents, and runs the commands on it, repeating for all lines of input (unless a command specifies otherwise).
The pattern space is not saved between lines, but what is is the hold space. The hold space is another buffer, that starts empty, and can be modified by program commands. Its contents are carried on to the execution of the next line of input.
The G
command appends a newline followed by the content of the hold space to that of the pattern space. Then the h
command replaces the hold space with the content of the pattern space. This effectively reverses the lines of input encountered so far, writing them to the pattern space – implicitly printing at the end of processing the current line – and saving them to the hold space so that upon reading subsequent lines of input, the new reversed "list" can be constructed with G;h
.
The ;
is escaped in the program as \;
because otherwise the shell interprets it as terminating a shell command.
1 2 1 3 2 1 4 3 2 1
), say, if a platform doesn't have a concept of multi-dimensional array/list? \$\endgroup\$