0

I have a sample text file as shown below. I want to read the file and print each line on the terminal. I wrote a sample shell script test_sample.sh.

The directory containing the shell script and text file contain others files too. When I run the shell script. It works fine, but the command output is mixed with output of the files residing in the directory.

% ./test_sample.sh sample.txt
== abc ==
abc.txt readme.txt sample.txt test_sample.sh I have a car.
abc.txt readme.txt sample.txt test_sample.sh My car is clean.

Question is how do I get to print just the lines of the text file?

test_sample.sh

#!/bin/bash
fname=$1
while read p; do
    echo $p
done < $fname

sample.txt

== abc == 
* I have a car.
* My car is clean.
Lokesh A. R.
  • 2,326
  • 1
  • 24
  • 28
  • 1
    It is just a matter of quoting the echo: `echo "$p"`. Otherwise, `*` gets expanded and shows all the files contained in your current directory. – fedorqui Jun 12 '14 at 13:23
  • Thanks @fedorqui! That worked. Whats difference when I used double quotes and no quotes? – Lokesh A. R. Jun 12 '14 at 13:26

0 Answers0