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

前端 未结 9 1480
梦如初夏
梦如初夏 2020-11-22 11:58

I\'m trying to do the opposite of \"Detect if stdin is a terminal or pipe?\".

I\'m running an application that\'s changing its output format because it detects a pip

9条回答
  •  忘了有多久
    2020-11-22 12:31

    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  
    

    Example:

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

提交回复
热议问题