Epistemic Status: seedling Sprouting - This is a zettelkasten-style card about a concept I’m interested in remembering.


Recently, I wanted to write a script that would query a data source, e.g. the AWS CLI, and output the results in Markdown format. When I ran it in the terminal, I wanted to run the output through a Markdown renderer so it looks nice, but if I piped it to a file, I wanted it to be the raw Markdown.

In Bash, we can detect when the script is connected to a TTY or if standard output is redirected either to a file or pipe.

if [[ -t 1 ]]; then
  echo "Outputting to TTY"
else
  echo "Output being redirected"
fi