Pretend to be a tty in bash for any command [duplicate]

There are a number of options, as outlined by several other Stack Overflow answers (see Caarlos’s comment). I’ll summarize them here though:

  1. Use script + printf, requires no extra dependencies:

     0<&- script -qefc "ls --color=auto" /dev/null | cat
    

    Or make a bash function faketty to encapsulate it:

     faketty () {
         script -qefc "$(printf "%q " "$@")" /dev/null
     }
     faketty ls --color=auto | cat  
    

    Or in the fish shell:

     function faketty
         script -qefc "(printf "%q " "$argv")" /dev/null
     end
     faketty ls --color=auto | cat 
    

    (credit goes to this answer)

    http://linux.die.net/man/1/script

  2. Use the unbuffer command (as part of the expect suite of commands), unfortunately this requires an extra package install, but it’s the easiest solution:

     sudo apt-get install expect-dev   # or brew install expect
     unbuffer -p ls --color=auto | cat  
    

    Or if you use the fish shell:

     function faketty
         unbuffer -p $argv
     end
     faketty ls --color=auto | cat 
    

    http://linux.die.net/man/1/unbuffer

This is a great article on how TTYs work and what Pseudo-TTYs (PTYs) are, it’s worth taking a look at if you want to understand how the linux shell works with file descriptors to pass around input, output, and signals. http://www.linusakesson.net/programming/tty/index.php

Leave a Comment