HowTo Display Text Files In a Shell Script

by on May 2, 2007 · 4 comments· LAST UPDATED November 23, 2013

in , ,

I am a new Unix user and learning bash shell scripting. How do I display a text file stored in /tmp directory in a shell script?

There are many ways to display a text file in a shell script. You can simply use the cat command and display back output on screen. Another option is to read a text file line by line and display back the output. In some cases you may need to store output to a variable and later display back on screen.
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Estimated completion time5m

Task: Display output using the cat command

Following sample script displays contents of a file called /tmp/input.txt:

echo "*** File - $FILE contents ***"
cat $FILE

Task: Store a file output in a variable

Following sample script displays nameserver IPs:

OUT=$(awk '{ print $2 }' $FILE)
echo "*** Nameserver IP ***"
for ip in $OUT
   echo $ip

Task: Read a text file line-by-line

See our previous tutorial for more information:

  1. Linux/ UNIX: Read a File Line By Line
  2. Linux / Unix AWK: Read a Text File
  3. HowTo: Bash For While Loop Through File Contents Script
  4. Ksh Read a File Line By Line ( UNIX Scripting )
Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 4 comments… read them below or add one }

1 shekar November 1, 2007 at 12:30 pm

2nd option is good. If file is large, one can modify the script a bit to serve the purpose.


2 Palash Khan R. December 19, 2007 at 9:25 pm

How to display certain part of a file? For example, I have a file myFile.Java and I would like to display the contents between lines 30-40. How to do that in linux? with cat?



3 mahesh October 16, 2013 at 1:22 pm

I have 1 more Que.

i want a shell script for identify mentioned string is file or directory if its a directory then display the contests of the same and if this is file show the content of file


4 NO CAT August 5, 2014 at 8:25 pm

cat $FILE

echo “$(<$FILE)"

tee <$FILE


Leave a Comment

Tagged as: ,

Previous Faq:

Next Faq: