Search code examples
shellunixless-unix

Why is it that we can redirect the input of 'less' command, but we can't run less without any arguments?


as far as i learned in programming, when i want to add the input redirection to my program, i write my program as when it executed, it waits for the user's input (with std::cin or something similar). but i got confused by less command.

We all know that we can do something like this:

ls -la | less

but when we try to execute

less

without any arguments we get an error. how come ?!


Solution

  • The less accepts input from stdin or from a file.

    Since it doesn't make sense to accept input from a terminal (just to display the same input back to a terminal), the less program probably checks if stdin is a terminal (with isatty) and refuses to run.

    See man 3 isatty