Skip to main content

How to Redirect Spectacles Output

You might want to pipe the output of Spectacles to another command line tool, or redirect it to a file.

Because Spectacles uses the Python logging library, it outputs all messages to stderr, not stdout.

This means if you try to redirect or pipe stdout to file (e.g. spectacles sql > file.txt), the file will be empty, because stdout is also empty.

Instead, you need to redirect stderr to the output file. If you're using bash or zsh, you can use the handy &> operator to redirect both stdout and stderr to a file. For example:

spectacles sql --config-file config.yaml &> output.txt

If you're not using a shell where &> is supported, you can instead redirect stdout to the file, then redirect stderr to stdout:

spectacles sql --config-file config.yaml >output.txt 2>&1

This is also how you would pipe stderr, by first redirecting it to stdout. For example, you can use tee so the output is displayed and redirected simultaneously. In this example, we're also ignoring stdout by sending it to /dev/null:

spectacles sql --config-file config.yaml >/dev/null 2>&1 | tee output.txt
tip

Spectacles adds color to some of the messages it outputs, which can show up as unwanted characters in places that don't support color, like plain text files. To disable color output, set the NO_COLOR environment variable:

export NO_COLOR=1