Check if script has been redirected
bash, io-redirection, shell
Solution
You can use `-t` option of bash:
-t fd True if file descriptor fd is open and refers to a terminal.
And check like this:
if [[ -t 1 ]]; then
# console is attached
else
# Redirected to somewhere
fi
Problem
I have a `bash` script which must run differently if the output is being shown in the terminal, or if it is redirected to a file. It must do something like this (`myscript.sh`): ``` if [ redirected_to_terminal ] ; then flag="--color" else flag="--no-color" fi grunt $flag ``` This would be called like this: ``` ./myscript.sh ``` Or like this: ``` ./myscript.sh > /tmp/log.txt ``` And the redirection of `stdout` would be detected in the script. Is this possible?