I want to have a section in my code that only executes, when the output is printed in the terminal, not piped or redirected into a text file or other program.
I tried this:
#!/bin/bash
if [[ $(tty "-s") ]]; then
printf "You are using a terminal.\n"
else
printf "You are not using a terminal.\n"
fi
tty -s && printf "Guten Tag.\n"
Output after ./tty.sh
command:
You are not using a terminal.
Guten Tag.
Output in test.txt after `./tty.sh > test.txt`:
``` Youre not using a terminal. Guten Tag. ```