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

后端 未结 4 1743
刺人心
刺人心 2021-02-15 16:53

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:26

    $'\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.

提交回复
热议问题