To build an interactive application you can open /dev/tty
, it will return a file descriptor to the controlling terminal:
int ttyfd = open("/dev/tty", O_RDWR);
You can use it instead of STDIN_FILENO
or STDOUT_FILENO
(those could be redirected to something different than the terminal when the program is started).
Here is some example:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
int ttyfd = open("/dev/tty", O_RDWR);
printf("fd: %d\n", ttyfd);
write(ttyfd, "hello tty!\n", 11);
return 0;
}
When invoked with ./test >out
, it should print the hello message on the terminal and something like fd: 3
in the out
file.
If grep o
produces color output, then either grep
is an alias to grep --color=auto
or grep --color=always
(or possibly more options), or GREP_OPTIONS
is set to a value that contains --color=auto
or --color=always
. Since $GREP_OPTIONS
is empty, it must be the alias.
Since grep o | less -R
doesn't show colors, the alias must be to grep --color=auto
(a sensible choice). With the alias, the grep
command always receives the --color
option on the command line, and this takes precedence over the environment variable.
If you want to use the environment variable, remove the alias definition from your ~/.bashrc
, or for one session run unalias grep
. You can replace alias grep='grep --color=auto'
by export GREP_OPTIONS='--color=auto'
: they have essentially the same meaning, except that:
- setting
GREP_OPTIONS
to a different value only overrides the latter;
- the alias only kicks in when you run
grep
from an interactive shell, whereas setting GREP_OPTIONS
also applies when grep
is run from scripts and other applications.
Never put --color=always
or most other options in GREP_OPTIONS
: it would break many programs that parse the output of grep
. --color=auto
is about the only safe option to put in GREP_OPTIONS
. For anything else, use the alias. Future versions of GNU grep will drop support for the option for this reason.
Note that the alias definition goes into ~/.bashrc
(it's a shell setting), whereas the environment variable definition goes into ~/.profile
(it's a session setting). See Is there a ".bashrc" equivalent file read by all shells?
If you want to run the unaliased command just once, run \grep
instead of grep
(quoting any part of the name bypasses the alias lookup).
Best Answer
The most obvious one is
cat
. But, also have a look athead
andtail
. There are also other shell utillities to print a file line by line:sed
,awk
,grep
. But those are to alternate the file content or to search inside the file.I made a few tests to estimate which is the most effective one. I run all trough
strace
to see which made the least system calls. My file has 1275 lines.awk
: 1355 system callscat
: 51 system callsgrep
: 1337 system callshead
: 93 system callstail
: 130 system callssed
: 1378 system callsAs you can see, even if
cat
was designed to concatenate files, it is the fastest and most effective one.sed
,awk
andgrep
printed the file line by line, that why they have more that 1275 system calls.