LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-08-2009, 01:48 PM   #1
gr8linux
Member
 
Registered: May 2009
Posts: 44

Rep: Reputation: 15
Post display contents of file


can we use echo command for displaying the contents of a particular file....................
 
Old 07-08-2009, 02:05 PM   #2
nuwen52
Member
 
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
You can use "cat" to display the contents of a file.
 
Old 07-08-2009, 02:17 PM   #3
NeddySeagoon
Gentoo support team
 
Registered: May 2009
Location: 56N 3W
Distribution: Gentoo
Posts: 178

Rep: Reputation: 40
gr8linux,

You can also use more and less, which display a file a page at a time, where page is whatever fits in the window. more only allows you to move forward in the file, less allows backwards scrolling too.
 
Old 07-08-2009, 02:28 PM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Yes echo can be used to display the contents of a file. The way I did it is by command substitution. The basic syntax is echo "`cat filename`". The contents of filename will be cat-ted and the standard output of cat will be substituted as a parameter for echo.

Code:
echo "`cat hello.c`"
#include <stdio.h>

int main( void ) {
  printf( "Hello, World!\n" );
  return 0;
}
There is an alternate syntax for command substitution using $() instead of back-quotes.

Code:
echo "$(cat hello.c)"
#include <stdio.h>

int main( void ) {
  printf( "Hello, World!\n" );
  return 0;
}
I have tried using redirections but it just doesn't work because echo does not receive its parameters from STDIN. Maybe someone else knows how to do it using redirections?
 
Old 07-08-2009, 02:47 PM   #5
abi0909
Member
 
Registered: Jul 2009
Distribution: Ubuntu, RedHat, VMWare, CentOS, Windows, Android, Mac
Posts: 55

Rep: Reputation: 18
When you can simply cat a file, why should we be using echo unless a specific purpose. I would always be simple.
 
Old 07-08-2009, 02:54 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by gr8linux View Post
can we use echo command for displaying the contents of a particular file....................
Heh ... what is it with your obsession with 'echo'? =D
More than 60% of your posts are about echo ...


Cheers,
Tink
 
Old 07-08-2009, 02:56 PM   #7
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Or, if you need to print only particular parts of a file, you could use other tools such as awk (which may be a little overkill though). An example of awk printing a whole file is pretty useless (just one print statement), and more useful examples would be too plenty, so if you're interested you should search for howto or some awk manual. There are many available on the net, and the examples should give you a quick overview of what it is; just don't get fooled, awk can do a lot more than the examples (especially one-liners) can show. What I typically do with it is extract column data from files or do calculations where the data involved needs to be extracted from certain lines/columns in the file, leaving other content out.

Anyway, in principle awk does just as well as cat, even if it is quite a bit more complex.
 
Old 07-08-2009, 05:14 PM   #8
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by abi0909 View Post
When you can simply cat a file, why should we be using echo unless a specific purpose. I would always be simple.
Only the OP could possibly answer that question. IMHO my own reply comes closest to answering his question. I do agree with everyone here that cat, more, less, and awk are all much better ways to simply display the contents of a file.
 
Old 07-08-2009, 05:36 PM   #9
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,072

Rep: Reputation: 384Reputation: 384Reputation: 384Reputation: 384
Quote:
Originally Posted by Tinkster View Post
Heh ... what is it with your obsession with 'echo'? =D
More than 60% of your posts are about echo ...


Cheers,
Tink
I was also wondering about that. Maybe he wants to start a new OS or something (echOS?).

Jokes apart, the only way I know is to use stdout as an argument for echo, as someone else pointed above, which is redundant and silly as also pointed above by someone else.

Code:
echo $(whatever)
You could invent lots of creative ways to do silly things with echo like

Code:
echo | xargs < myfile.txt
And so on, that doesn't solve the main point anyway: you should be using cat.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to display text file contents in Xdialog's radiolist or menubox kkpal Linux - Newbie 0 09-09-2008 02:18 AM
Display file contents in table format dresch Linux - Newbie 1 01-04-2007 09:35 AM
Apache2: How to allow Apache to display contents in the file system? vous Linux - Software 1 02-03-2004 05:36 AM
I want ls to not display contents of directories GameboyHippo Linux - Newbie 2 01-26-2004 03:47 AM
Konqueror won't display Windows FTP contents tx29219 Linux - Software 1 02-27-2003 09:46 AM


All times are GMT -5. The time now is 11:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration