
For a list of BASHing data 2 blog posts see the index page.
The script command for tinkerers
I like to tinker on the command line, and if something interesting (or bad!) happens I like to review what I've been doing. One way to do that is to go back through my BASH history, but if I've been using other commands in the meantime, the relevant bits of the history might be scattered and hard to find.
For this reason I use the script command to record tinkering sessions. I do
script -q -a tinker
do tinkering stuff
exit
and repeat until I'm happy with the result. The -q option to script suppresses program messages while I'm tinkering, and -a appends new tinkering commands to the text file "tinker", which is in my default working directory. The "exit" command stops script from recording what's in the terminal.
For a demonstration, I did
script -q -a tinker
printf "aaa\n"
exit
echo "blah"
then closed my terminal. Opening it again I did
script -q -a tinker
printf "aaa\n" | sed 's/a/BB/2'
exit
echo "blah"
and closed the terminal again. A bit later I opened the terminal for a look at "tinker":

Cleaning the extraneous lines with AWK makes that easier to read:
awk '!/Script/&& !/exit/' tinker

I can save that result to a text file, but there's a problem:
awk '!/Script/&& !/exit/' tinker > tinker-awked

While the strings marked by escapes could be removed in this example with ansi2txt, the lines formerly occupied by "ESC[?2004l" will remain as blank lines. What's worse, script records everything you do, including backspaces and up- and down-arrowings. Those are very fiddly to remove with text tools.
The safest workaround I've found for saving a script output file to text is to mouse-select the output from the terminal (e.g., from "cat tinker"), copy it, paste into a text editor and then do the AWK cleaning.
Next post:
2025-07-18 Format musings 1: NestedText and indentation
Last update: 2025-07-11
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License