-1

I have a text file with the following contents,

My test
strings
that dont have
a question 
mark except this line?
but not
these two

and when i try to read the file in bash using, for example,

ph_lines="/path/to/file.txt"
for l in $(cat "$ph_lines") 
do
    echo "$l"
done

everything prints on the output except for the string with the question mark in it.

I have tried using while read line; echo line; done < $filename and it still has the same problem

The only thing that would work to capture all of the lines is when i used sed to remove question marks.

for l in $(cat ${ph_lines} | sed $'s/\?//' )

Thank you!

  • You didn't prefix your variable with `$` as in: `while read line; echo "$line"; done < filename` – SiegeX May 30 '20 at 23:32
  • 3
    https://shellcheck.net for validating your script. – Jetchisel May 30 '20 at 23:34
  • https://mywiki.wooledge.org/BashFAQ/001 is useful reading. – Shawn May 30 '20 at 23:38
  • Welcome to StackOverflow! The code in your post does not have the problem you describe. Your actual script has something to the effect of `IFS=$'\n'; shopt -s nullglob;` in it, which is necessary to reproduce the issue. Protip: the easiest way to ensure your question is complete and captures all necessary details is to copy-paste the code from your post into a new file and testing that first – that other guy May 30 '20 at 23:49
  • I'm not able to reproduce the issue. The word with the question mark is printed the same way as all other ones. – choroba May 30 '20 at 23:56

1 Answers1

0

To correctly read from a file, use the example from Read a file line by line assigning the value to a variable:

while IFS= read -r line; do
    echo "$line"
done < my_filename.txt

The difference is not that it's a while loop (because you've tried that), but because this loop is correctly quoted. The problem you're seeing happens because you secretly enabled nullglob first, and then neglected to quote:

$ shopt -s nullglob
$ var='question?'

$ echo "$var"
question?

$ echo $var
(blank line)

Unquoted expansion causes pathname expansion, and since you enabled nullglob and have no matching files, the previous example shows nothing. If you had some matching files, you'd see those instead:

$ touch questions question2

$ echo $var
question2 questions

You can set up shellcheck in your editor to get automatic warnings about these issues.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • tbh dont even know why i have shopt -s nullglob turned on. A lot of the script i'm writing is copy pasted from some snips here and there. I just learned about spellcheck... I have a lot of issues. Thanks for the info and help! – Bjorn Sodergren May 31 '20 at 00:12
  • There is a minor issue with proposed solution - for the case that the input file has lines starting with '-', which could activate any of the `echo` command line options (e.g., '-e'). Consider using 'printf '%s\n' "$line"` instead. too bad `echo` does not support `--` to mark the end of the options, which is the common way to indicate end of options. – dash-o May 31 '20 at 03:17