10

When I do ls /dev/tty*, I see the following output:

/dev/tty    /dev/tty12  /dev/tty17  /dev/tty21  /dev/tty26  /dev/tty30  /dev/tty35  /dev/tty4   /dev/tty44  /dev/tty49  /dev/tty53  /dev/tty58  /dev/tty62  /dev/ttyS0
/dev/tty0   /dev/tty13  /dev/tty18  /dev/tty22  /dev/tty27  /dev/tty31  /dev/tty36  /dev/tty40  /dev/tty45  /dev/tty5   /dev/tty54  /dev/tty59  /dev/tty63  /dev/ttyS1
/dev/tty1   /dev/tty14  /dev/tty19  /dev/tty23  /dev/tty28  /dev/tty32  /dev/tty37  /dev/tty41  /dev/tty46  /dev/tty50  /dev/tty55  /dev/tty6   /dev/tty7   /dev/ttyS2
/dev/tty10  /dev/tty15  /dev/tty2   /dev/tty24  /dev/tty29  /dev/tty33  /dev/tty38  /dev/tty42  /dev/tty47  /dev/tty51  /dev/tty56  /dev/tty60  /dev/tty8   /dev/ttyS3
/dev/tty11  /dev/tty16  /dev/tty20  /dev/tty25  /dev/tty3   /dev/tty34  /dev/tty39  /dev/tty43  /dev/tty48  /dev/tty52  /dev/tty57  /dev/tty61  /dev/tty9

The formatting is well and I can see all the files in modest block inside terminal.
But when I run this command such a way watch -d -n1 'ls /dev/tty*', I see:

Every 1.0s: ls /dev/tty*                                                                                                                                                        debian: Wed Jun 30 21:08:06 2021

/dev/tty
/dev/tty0
/dev/tty1
/dev/tty10
/dev/tty11
/dev/tty12
/dev/tty13
/dev/tty14
/dev/tty15
/dev/tty16
/dev/tty17
/dev/tty18
/dev/tty19
/dev/tty2
/dev/tty20
/dev/tty21
...

So the output listed in vertical and doesn't fit my screen. What is the reason? How can I solve this?

2
  • So you want the output in watch to be the same as in regular ls?
    – budoattack
    Jun 30, 2021 at 18:37
  • @JeffSchaller deleted his answer after thinking it would be wrong, but I think it was the right solution, adding option ´-x´ to ´ls´. Jul 1, 2021 at 6:36

2 Answers 2

11

What is the reason?

When watch executes commands they are not connected to the terminal. In other words, isatty(3) returns 0. You can use the following isatty.c to check if a command is connected to the terminal when it's ran:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
  printf("%d\n", isatty(STDOUT_FILENO));

  return EXIT_SUCCESS;
}

Compile:

gcc isatty.c -o isatty

Run in your terminal emulator:

$ ./isatty
1

Run it in watch:

$ watch ./isatty
Every 2.0s: ./isatty                                                                                                                                                              darkstar: Wed Jun 30 20:42:51 2021

0

How can I solve this?

Use -C option with ls in watch:

watch -d -n1 'ls -C /dev/tty*'
1
  • 5
    You don't need a new program, just something like test -t 1 && echo tty || echo not Jul 1, 2021 at 1:10
0

The answer given above is fine, since ls offers an option to perform the same kind of work it does when it detects a TTY. But in case you ever want to do this on some other command - that doesn't have such an option - an easy way is this:

$ watch 'unbuffer ls /dev/tty*'

unbuffer [1] basically creates a tty, and uses it to stop the buffering done otherwise. A side effect, is that the programs it runs (in this case, ls) see a TTY - and therefore will do exactly what you would see in your terminal.

[1] https://linux.die.net/man/1/unbuffer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .