• Re: read, REPLY vs. user variable

    From marrgol@21:1/5 to All on Tue Aug 22 10:13:03 2023
    On 2023-08-22 at 09:18 JaceK wrote:
    I'd like to read input into variable and check if it contains TAB character. Here are two version of the code:

    while read l && echo "$l" | grep -P '\t' ; do echo "$l $?"; done

    while read && echo "$REPLY" | grep -P '\t' ; do echo "$REPLY $?"; done

    Both versions work as desired if TAB is somewhere in the middle of the entered line.

    Version with REPLY works as desired if TAB is at the end/beginning of the line.

    Version with user variable doesn't work as desired if TAB is at the end/beginning of the line.

    What do I miss? Why does it seem like version with REPLY can handle user input no matter the position of the TAB character and similar code with user variable works differently?

    Quoting bash man page where it describes the ‘read’ command: “The characters
    in IFS are used to split the line into words using the same rules the shell uses for expansion […]”.

    By default IFS contains SPACE, TAB and NEWLINE, so (multiple) SPACEs
    and TABs are removed from the beginning and the end of the input line.
    You need to eliminate TAB from IFS for your code to work as expected:

    while IFS=$' \n' read l && echo "$l" | grep -P '\t' ; do echo "$l $?"; done

    I guess REPLY contains the whole input line as entered, without any interpretation.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From JaceK@21:1/5 to All on Tue Aug 22 09:18:27 2023
    I'd like to read input into variable and check if it contains TAB
    character. Here are two version of the code:

    while read l && echo "$l" | grep -P '\t' ; do echo "$l $?"; done

    while read && echo "$REPLY" | grep -P '\t' ; do echo "$REPLY $?"; done

    Both versions work as desired if TAB is somewhere in the middle of the
    entered line.

    Version with REPLY works as desired if TAB is at the end/beginning of
    the line.

    Version with user variable doesn't work as desired if TAB is at the end/beginning of the line.

    What do I miss? Why does it seem like version with REPLY can handle user
    input no matter the position of the TAB character and similar code with
    user variable works differently?

    JaceK

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to JaceK on Tue Aug 22 11:26:29 2023
    On 22.08.2023 09:18, JaceK wrote:
    I'd like to read input into variable and check if it contains TAB
    character. Here are two version of the code:

    while read l && echo "$l" | grep -P '\t' ; do echo "$l $?"; done

    while read && echo "$REPLY" | grep -P '\t' ; do echo "$REPLY $?"; done

    Both versions work as desired if TAB is somewhere in the middle of the entered line.

    Version with REPLY works as desired if TAB is at the end/beginning of
    the line.

    Version with user variable doesn't work as desired if TAB is at the end/beginning of the line.

    What do I miss? Why does it seem like version with REPLY can handle user input no matter the position of the TAB character and similar code with
    user variable works differently?

    Note that results differ depending on the used shell.

    Unless you have multiple variables to read in one 'read' command (or deliberately want to strip leading/trailing separators) I suggest to
    clear the field separator on reading (in both cases)

    while IFS= read ...


    Janis


    JaceK

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From JaceK@21:1/5 to marrgol on Tue Aug 22 16:12:19 2023
    On 8/22/23 10:13, marrgol wrote:
    On 2023-08-22 at 09:18 JaceK wrote:
    I'd like to read input into variable and check if it contains TAB character. >> Here are two version of the code:

    while read l && echo "$l" | grep -P '\t' ; do echo "$l $?"; done

    while read && echo "$REPLY" | grep -P '\t' ; do echo "$REPLY $?"; done

    Both versions work as desired if TAB is somewhere in the middle of the
    entered line.

    Version with REPLY works as desired if TAB is at the end/beginning of the line.

    Version with user variable doesn't work as desired if TAB is at the
    end/beginning of the line.

    What do I miss? Why does it seem like version with REPLY can handle user
    input no matter the position of the TAB character and similar code with user >> variable works differently?

    Quoting bash man page where it describes the ‘read’ command: “The characters
    in IFS are used to split the line into words using the same rules the shell uses for expansion […]”.

    By default IFS contains SPACE, TAB and NEWLINE, so (multiple) SPACEs
    and TABs are removed from the beginning and the end of the input line.
    You need to eliminate TAB from IFS for your code to work as expected:

    while IFS=$' \n' read l && echo "$l" | grep -P '\t' ; do echo "$l $?"; done

    I guess REPLY contains the whole input line as entered, without any interpretation.


    Thank you! Both of you, Janis too.

    JaceK

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)