3 ms·
I would recommend against using $REPLY, for two related reasons: 1. It’s confusing to do a plain “read” and later have a magic $REPLY variable show up, seeming
by teddyh 8y ago
I would recommend against using $REPLY, for two related reasons:
1. It’s confusing to do a plain “read” and later have a magic $REPLY variable show up, seemingly from nowhere. It has shades of Perl, and I mean nothing positive with that statement.
2. Names are good. By setting a name to the thing which is read, you make the later code easier to read. It’s easier to understand “echo $filename” than “echo $REPLY”, so if you’re reading filenames, do “read filename”. (Or, rather, you probably want to do “read -r filename”, but that’s a different subject.)
- daveFNbuck 8y agoI don't do a lot of bash programming, but I imagine using $REPLY also increases the danger of having the value overwritten before you use it as your code changes.
- mar77i 8y agoNow that you mentioned it, I never hit the case where I would read two different times in the same function/subshell scope and would never have considered this a problem. But maybe I'm just preempting this halfway subconciously applying meticulous scoping?
- mar77i 8y agoThere's an actual difference in behavior between`read name` and `read`: one strips leading and trailing whitespace, the other does not. I know it because I once needed to strip whitespace and only found out through IRC; the difference is only somewhat hinted at in the manual.
- teddyh 8y agoIn the case where you want to preserve whitespace, you probably want to do this, then: read -r foo="$REPLY"
- noisy_boy 8y agoYeah I don't use it in practice - I remembered it because it was part of my bash trivia collection when I started learning more about shell scripts many years go. Named variables are obviously a cleaner choice.