In a bash script, how can I determine if the output of a script is redirected to a file?

I want to write a shell script that will use colored output when the output is terminal, and normal output when redirected to a file. How can i do this?

+6
source share
1 answer

Very simple:

if [ -t 1 ]; then echo "Hello, terminal." else echo "Not a terminal." fi 

-t checks if the given file descriptor is attached (here 1 = stdout).

+9
source

Source: https://habr.com/ru/post/981962/


All Articles