If we simply use “ls” command, file names are listed in multi columns. But, if we redirect output to any file like “ls > foo”, it lists file names in single column. What is the reason for this behavior?
ls or any program can determine if you are on an interactive-shell or not. In other terms, it is possible for the program to determine if you have a terminal associated with your particular login when you run the program. If there is terminal, ls will produce multi-column output and if it is not there, it will produce long listing.
Try this. It will show you a working example:
ssh localhost ls
ssh -t localhost ls
In the first case, there is no terminal associated with the ssh login and
therefore you should get a long listing. In the second, we are explicitly
allocating terminal with -t option and there you should get multi column output.
In perl, it is very common to use something like
print “” if -t STDIN;
This will print the debug output only if it runs from a terminal.
Note: The above comments are taken from the shell scripting community at orkut. Just copied to here as I thought that would be helpful.