What Does the -T Flag Mean in a Shell Script If Statement?

I'm not sure what the -t flag means for unix shell scripts since I can't find any mention of it online.

I ran into an if statement for a bash shell shell script. I've included a sanitized snippet:

source "${BASH_SOURCE%/*}"/some_script

if [[ ! -t 0 ]] && [[ $(id -u) == 0 ]]; then
    echo 'got here'
else
    echo 'no get here'
fi

I don't understand what the "-t" flag in the test command. I assume it doesn't have anything to do with testing files since it didn't appear with the other file testing flags I found and because 0 is not a file.

Thanks in advance.

1 Answer

-t means:

True if file descriptor is open and refers to a terminal.

In this case, file descriptor 0 is standard input, so it's checking to see if standard input is coming from the terminals.

For a complete list of these file descriptors, run man bash and search for "CONDITIONAL EXPRESSIONS".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest