Home > Software design >  What does the -t flag mean in a shell script if statement?
What does the -t flag mean in a shell script if statement?

Time:05-16

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.

CodePudding user response:

-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".

  • Related