0

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?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
duggi
  • 556
  • 5
  • 13
  • Which `man` page did _not_ contain the description for those flags? – Inian Dec 17 '19 at 05:46
  • 3
    The `<<<` is a bashism called the *Herestring* and it redirects the variable `$count` as if it were a file on `stdin` allowing it to be read by `read` - which in this case reads ignoring escapes (`-r`) until the delimiter (`-d ' '`) (space) is found. Type `man bash` then `3813g` return (and look for `read`) That will get you close to the correct line. Or you can just type `/-ers` return and it will take you there. – David C. Rankin Dec 17 '19 at 05:46
  • 1
    Thanks @DavidC.Rankin. I was looking for -r and -d in `man read`. – duggi Dec 17 '19 at 06:37
  • Glad to help. `man bash` is a bit frightening the first couple of times to peer inside. But after a while you figure out it is laid out in handful of sections that you can usually jump to and find what you need. A 30 min. read of the entire `EXPANSION` section will save you 100-fold that amount of time later in your use of bash `:)` (especially the `"Parameter Expansion"` sub-section) – David C. Rankin Dec 17 '19 at 06:43
  • Depending on your platform, `man read` might bring up a man page from [section 2](https://linux.die.net/man/2/intro) of the manual; on my Mac, it gets redirected to [man builtins](https://linux.die.net/man/1/builtins) which appears to be the behavior at least on some Linux platforms now too. – tripleee Dec 17 '19 at 08:52
  • I upvoted to counter attack the phantom downvoter. This is a perfectly good question and reading all these comments is super informative and entertaining. Which one would assume is the whole point of SO. – Matias Barrios Dec 17 '19 at 12:23

1 Answers1

2

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Does this mean the above code is equivalent to `echo $count | read -rd count`? – duggi Dec 17 '19 at 06:38
  • Not exactly, because `read` in a subprocess (as in, at the end of a pipeline) generally doesn't work; and you forgot the argument to the option `-d`, which requires one; and also you failed to quote `echo "$count"`. But that's the idea, yes. – tripleee Dec 17 '19 at 06:51
  • 1
    Also, some versions of `echo` can mangle or misinterpret some strings. Re the original question: I’m curious what this actually accomplishes (other than adding a newline to the string). – Gordon Davisson Dec 17 '19 at 08:50