In a bash script, what would $'\0' evaluate to and why?

后端 未结 4 1542
孤独总比滥情好
孤独总比滥情好 2021-02-15 17:08

In various bash scripts I have come across the following: $\'\\0\'

An example with some context:

while read -r -d $\'\\0\' line; do
    ec         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-15 17:43

    $'\0' expands the contained escape sequence \0 to the actual characters they represent which is \0 or an empty character in shell.

    This is BASH syntax. As per man BASH:

    Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Known backslash escape sequences are also decoded.

    Similarly $'\n' expands to a newline and $'\r' will expand to a carriage return.

提交回复
热议问题