Mohammedz.com

For Linux and Shell scripting.

Shell command outputs: multi-column and single column outputs

1 Comment


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.

Advertisements

One thought on “Shell command outputs: multi-column and single column outputs

  1. Pingback: Shell command outputs: multi-column and single column outputs - Orkutadda.com - Orkut scraps, orkut graphics, orkut, scraps, html, graphics, glitter, codes, tools, layouts, profiles, scrapbook

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s