Website Logo. Upload to /source/logo.png ; disable in /source/_includes/logo.html

Mocking Eye

'Tis all in vain?

Reading Everything Stdin in a Bash Script

| Comments

In trying to get a collectd instance to send me alerts, I found out that I didn’t know how to just read everything that might be piped into a bash script via standard input. You can read line by line easily using the read command. Just looping over it seemed pretty horrendous to me until I discovered an elegant way to do it that’s perhaps canonical, but doesn’t seem to pop up via quick googling:

1
2
3
4
5
#!/bin/bash

VALUE=$(cat)

echo "$VALUE"

That’s it! $(cat) is a shorthand for $(cat /dev/stdin), so presumably you can use this to read from stderr by pointing it at /dev/stderr.

Happy piping!

Comments