How to trick an application into thinking its stdout is a terminal, not a pipe How to trick an application into thinking its stdout is a terminal, not a pipe bash bash

How to trick an application into thinking its stdout is a terminal, not a pipe


Aha!

The script command does what we want...

script --return --quiet -c "[executable string]" /dev/null

Does the trick!

Usage: script [options] [file]Make a typescript of a terminal session.Options: -a, --append                  append the output -c, --command <command>       run command rather than interactive shell -e, --return                  return exit code of the child process -f, --flush                   run flush after each write     --force                   use output file even when it is a link -q, --quiet                   be quiet -t[<file>], --timing[=<file>] output timing data to stderr or to FILE -h, --help                    display this help -V, --version                 display version


Based on Chris' solution, I came up with the following little helper function:

faketty() {    script -qfc "$(printf "%q " "$@")" /dev/null}

The quirky looking printf is necessary to correctly expand the script's arguments in $@ while protecting possibly quoted parts of the command (see example below).

Usage:

faketty <command> <args>

Example:

$ python -c "import sys; print sys.stdout.isatty()"True$ python -c "import sys; print sys.stdout.isatty()" | catFalse$ faketty python -c "import sys; print sys.stdout.isatty()" | catTrue


The unbuffer script that comes with Expect should handle this ok. If not, the application may be looking at something other than what its output is connected to, eg. what the TERM environment variable is set to.