bashherestring

What does this bash script involving "read" do?


I found this in one of the bash scripts for capturing metrics running on CentOS.

read  -rd '' count <<< "$count"

I know read reads the content of file descriptor into buffer, but I cannot find the documentation for the command line switches -r, -d.

Additionally, What does triple left arrow <<< do?


Solution

  • All of these are Bash features which you will find amply documented in the Bash manual.

    <<< is "here string" format; it's sort of like a here document:

    cat <<____HERE
        Hello, World!
    ____HERE
    

    ... except the token after the separator is the actual string to pass in as standard input to the command.

    The -r option to read disables some legacy behavior with backslashes from the original Bourne shell.

    -d sets the record delimiter. An empty string says to stop reading when you get a NUL character.