I want to experiment with terminals and colored output, but at the same time I want to make stdout/stderr redirection to file possible without having to deal with these escape sequences.
Is there any way to check whether stderr
is connected to a terminal from within a shell script?
The file descriptor for standard error, stderr, is 2. The test
or [
command (which is often a shell built-in) has an option, -t
, to test whether a file descriptor is connected to a terminal:
if [ -t 2 ]
then : stderr is a terminal
else : stderr is not a terminal
fi
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.