0
#!/bin/bash

while read p
do
  echo "$p"
done < numbers.txt

This script is only reading and allowing me to print .sh files. I have tried it with .txt files (like above but it does not print anything.)

8
  • Please add a sample of the contents of numbers.txt. The filename suffix means nothing on Unix systems. Both .sh and .txt files are just text files.
    – Kusalananda
    Feb 18, 2019 at 19:26
  • I added a screen shot of both files Feb 18, 2019 at 19:29
  • 2
    Please, don't post images of text.
    – Kusalananda
    Feb 18, 2019 at 19:32
  • 1
    Your file does not contain a single terminated line. read can only read complete lines.
    – Kusalananda
    Feb 18, 2019 at 19:33
  • 1
    I guess it would be more correct to say that read reads the line (and assigns it to variable p) but returns false, having encountered EOF while doing so: hence the body of the while loop is not executed. Feb 18, 2019 at 19:40

1 Answer 1

0

That's because you are printing test not variable p Try this:

#!/bin/bash

while read p
do
  echo "$p"
done < numbers.txt

Test:

$ cat numbers.txt
1
2
345
678
9
$ bash script.sh
1
2
345
678
9
$
2
  • my apologies. I forgot to edit that ou, I was just testing something there. But even when changing to echo "$p", nothing prints Feb 18, 2019 at 19:24
  • @CesaSalaam It works for me, are you sure that numbers.txt file is not empty?
    – Matej
    Feb 18, 2019 at 19:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.