0

To automate access to my scanner, I'm trying to cat the output of scanimage -L into a Bash variable. In fact I was successfully doing this until recently with siL="$(scanimage -L | grep net | awk '{ print $2 }' )" in my Scan.sh, getting me addresses like hpaio:/net/ENVY_5530_series?ip=192.168.43.249 stored in $siL. I've today noticed an odd glitch which I've reduced to a minimum example here following.

Given a file lines containing just this text:

LINE1
LINE2_this_string_containing_an_embedded_question_mark_-> here?itis <-_doesn't_appear_if_cat_is_invoked_via_command_substitution
LINE3

cat lines correctly shows the file, but

$ L="$(cat lines)"; echo $L
LINE1 LINE2_this_string_containing_an_embedded_question_mark_-> <-_doesn't_appear_if_cat_is_invoked_via_command_substitution LINE3

(and the same if I replace cat with <). Why is that word containing the question mark omitted?

joharr
  • 144
  • 2
  • 7
  • 1
    Your exact example works correctly for me. However, you really should double quote your variables when you use them - `echo "$L"` in this instance – roaima Nov 18 '20 at 10:00
  • 1
    Possible duplicate of [Why does my shell script choke on whitespace or other special characters?](//unix.stackexchange.com/q/131766). Quote your expansions! – Stéphane Chazelas Nov 18 '20 at 10:02
  • Here, if `here?itis` disappears and there's no `herexitis` file in the current directory, it's likely you have the `nullglob` option enabled. I wouldn't recommend setting that option generally. See also [Why is nullglob not default?](//unix.stackexchange.com/q/204803) – Stéphane Chazelas Nov 18 '20 at 10:18

0 Answers0