TIL - interactive shell script

Posted by Marcus Folkesson on Thursday, August 29, 2024

TIL - interactive shell script

TIL, Today I Learned, is more of a "I just figured this out: here are my notes, you may find them useful too" rather than a full blog post

I saw this line in a script:

1INTERACTIVE="" && [[ -t 0 ]] && INTERACTIVE="-it"

And later on start a container:

1docker run --rm ${INTERACTIVE} -v "${PWD}":/home/br-user \
2    --user "$(id -u)":"$(id -g)" \
3    myproject/docker "${@}"

It tests if STDIN is opened on a terminal and in that case set $INTERACTIVE to -ti to start the container with an interactive shell.

I usually don't make a difference in how the behavior of a script depends on how it is started, I don't really like such things it at all, but I can still see that it has its advantages.

From the manual of test [1]:

-t FD  file descriptor FD is opened on a terminal