postprocessor, which may perform any desired clean‐up action (such as deleting the replacement file created by LESSOPEN).
This program receives two command line arguments, the original filename as entered by the user, and the name of the replacement file. To set up an input postprocessor, set the LESSCLOSE environment variable to a com‐
mand line which will invoke your input postprocessor. It may include two occurrences of the string "%s"; the first is replaced with the original name of the file and the second with the name of the replacement file,
which was output by LESSOPEN.
For example, on many Unix systems, these two scripts will allow you to keep files in compressed format, but still let less view them directly:
lessopen.sh:
#! /bin/sh
case "$1" in
*.Z) TEMPFILE=$(mktemp)
uncompress -c $1 >$TEMPFILE 2>/dev/null
if [ -s $TEMPFILE ]; then
echo $TEMPFILE
else
rm -f $TEMPFILE
fi
;;
esac
lessclose.sh:
#! /bin/sh
rm $2
To use these scripts, put them both where they can be executed and set LESSOPEN="lessopen.sh %s", and LESSCLOSE="lessclose.sh %s %s". More complex LESSOPEN and LESSCLOSE scripts may be written to accept other types
of compressed files, and so on.
It is also possible to set up an input preprocessor to pipe the file data directly to less, rather than putting the data into a replacement file. This avoids the need to decompress the entire file before starting to
view it. An input preprocessor that works this way is called an input pipe. An input pipe, instead of writing the name of a replacement file on its standard output, writes the entire contents of the replacement file
on its standard output. If the input pipe does not write any characters on its standard output, then there is no replacement file and less uses the original file, as normal. To use an input pipe, make the first
character in the LESSOPEN environment variable a vertical bar (|) to signify that the input preprocessor is an input pipe. As with non‐pipe input preprocessors, the command string must contain one occurrence of %s,
which is replaced with the filename of the input file.
For example, on many Unix systems, this script will work like the previous example scripts:
lesspipe.sh:
#! /bin/sh
case "$1" in
*.Z) uncompress -c $1 2>/dev/null
;;
*) exit 1
;;
esac
exit $?
To use this script, put it where it can be executed and set LESSOPEN="|lesspipe.sh %s".
Note that a preprocessor cannot output an empty file, since that is interpreted as meaning there is no replacement, and the original file is used. To avoid this, if LESSOPEN starts with two vertical bars, the exit
status of the script becomes meaningful. If the exit status is zero, the output is considered to be replacement text, even if it is empty. If the exit status is nonzero, any output is ignored and the original file
is used. For compatibility with previous versions of less, if LESSOPEN starts with only one vertical bar, the exit status of the preprocessor is ignored.
When an input pipe is used, a LESSCLOSE postprocessor can be used, but it is usually not necessary since there is no replacement file to clean up. In this case, the replacement file name passed to the LESSCLOSE post‐
processor is "-".
For compatibility with previous versions of less, the input preprocessor or pipe is not used if less is viewing standard input. However, if the first character of LESSOPEN is a dash (-), the input preprocessor is
used on standard input as well as other files. In this case, the dash is