Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

tty

Whenever I use grep, and I pipe it to an other program, the --color option is not respected. I know I could use --color=always, but It also comes up with some other commands that I would like to get the exact output of that command as the output I would get if I was in a tty.

So my question is, is it possible to trick a command into thinking that the command is run inside a tty ?

For example, running

grep --color word file # Outputs some colors grep --color word file | cat # Doesn't output any colors 

I'd like to be able to write something like :

IS_TTY=TRUE grep --color word file | cat  # Outputs some colors 

This question seems to have a tool that might do what I want :empty - run processes and applications under pseudo-terminal (PTY), but from what I could read in the docs, I'm not sure it can help for my problem

like image 803
edi9999 Avatar asked Oct 02 '15 15:10

edi9999


People also ask

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

What is &2 in bash script?

and >&2 means send the output to STDERR, So it will print the message as an error on the console. You can understand more about shell redirecting from those references: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Redirections.

Does bash run commands in parallel?

GNU parallel examples to run command or code in parallel in bash shell. From the GNU project site: GNU parallel is a shell tool for executing jobs in parallel using one or more computers. A job can be a single command or a small script that has to be run for each of the lines in the input.

What is TTY shell?

The tty command of terminal basically prints the file name of the terminal connected to standard input. tty is short of teletype, but popularly known as a terminal it allows you to interact with the system by passing on the data (you input) to the system, and displaying the output produced by the system.


1 Answers

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 " "$@")"  }  faketty ls --color=auto | cat   

    Or in the fish shell:

     function faketty      script -qefc "(printf "%q " "$argv")"  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

like image 104
Nick Sweeting Avatar answered Sep 25 '22 10:09

Nick Sweeting