Printing Output and Reading Command Status

A command reports results in two ways: the data it writes to its output, and an exit status, a number where zero means success. Scripts use output for data and status for decisions. These commands are the building blocks for both. The man page for each command lists every option.

Your shell has its own versions of some of these commands, such as echo, printf, test, and kill. To run the utility itself, put peiosutils before its name, as some examples here do.

The examples use sample file names such as report.txt. Create those files first, or substitute your own.

echo prints its arguments, which suits a short fixed message:

peiosutils echo 'Build complete'

For any value that comes from elsewhere, use printf with a fixed format, so that the value can't be read as an option or as formatting:

peiosutils printf '%s: %d\n' Aster 3

Keep the format under your control and pass data as the later arguments. The format repeats if more arguments remain.

seq 2 2 8 prints numbers from 2 to 8 in steps of 2, and yes sample prints a line repeatedly until it is stopped. Don't pipe yes into a program to answer prompts you haven't read: use the program's own option for running without prompts. numfmt --to=iec-i shortens a byte count with a binary prefix, which scales by powers of 1024. The result is rounded, so keep the exact number for any calculation.

Run a command with a changed environment #

env runs one command with an extra or changed environment variable, without changing your shell:

env PEIOS_EXAMPLE=ready peiosutils printenv PEIOS_EXAMPLE

printenv prints one variable when you name it, and fails if the variable doesn't exist, which tells an empty value apart from a missing one. Without a command, env prints the whole environment, which can contain credentials, so don't share that output. A cleared environment helps make a command repeatable, but it isn't a security boundary.

Test conditions #

test reports a condition through its exit status and prints nothing:

if peiosutils test -f report.txt; then
  peiosutils printf '%s\n' 'Report is a regular file'
fi

Quote variables so that an empty value stays one argument. A file test describes one moment: the file can change before you use it. true and false exist only for their status. Don't add || true to hide a failure unless you've handled the failure you expect.

expr 7 + 5 evaluates a small integer expression; quote operators such as * that the shell would interpret. factor 84 prints the prime factors of a number.

Read exit statuses #

A false condition isn't always an error. expr, for example, uses one status for a zero or empty result, another for a true result, and a third for an invalid expression. In a pipeline, each command has its own status, and the shell decides which one the pipeline reports. Read a command's man page before treating every nonzero status the same way.

Save a stream while showing it #

tee writes its input both to standard output and to a file:

printf 'Starting\nFinished\n' | tee progress.log

tee replaces the file's contents unless you use append mode. A new file inherits security from its directory, and an existing file keeps its own, so choose the directory with care when the stream holds private data.

Commands on this page #

This page covers these commands:

CommandUse it toManual
echoPrint a short messageecho(1)
printfPrint values with a fixed formatprintf(1)
seqPrint a sequence of numbersseq(1)
yesPrint a line repeatedlyyes(1)
numfmtFormat numbers for people to readnumfmt(1)
envRun a command with a changed environmentenv(1)
printenvPrint an environment variableprintenv(1)
testCheck a conditiontest(1)
true, falseSucceed or failtrue(1), false(1)
exprEvaluate a small expressionexpr(1)
factorPrint prime factorsfactor(1)
teeCopy a stream to a file and to outputtee(1)

Edit this page