3 ms·
A little heads-up: "$var" does what you think, but "$(cmd)" likely does not do what you think: - The former just gives you a string whose contents are identica
by mehrdadn 9y ago
A little heads-up: "$var" does what you think, but "$(cmd)" likely does not do what you think:
- The former just gives you a string whose contents are identical to that of var.
- The latter would do similar for the output of cmd, except that it strips away the trailing newline. This is often not an issue, but can be crucially important in some cases, and can catch you off-guard.
The point I'm making here is that it's actually quite difficult to get a string that literally has the contents you want. The fact that *nix lets you put pretty much any characters in file names (even newlines) means that, just like in Windows, your scripts can actually fail even when you try to quote things "properly".
- dozzie 9y ago> The point I'm making here is that it's actually quite difficult to get a string that literally has the contents you want. It's not difficult, it's just tedious. foo=$(whatever). foo=${foo%.}
- mehrdadn 9y agoThat's not general POSIX, right? I seem to recall it's Bash-specific? (P.S. I think you forgot quotes?) The other problem (which I guess I accidentally brushed under the rug when I singled out "variables") is that having to do this actually means you need to put it in a variable. If you're nesting subshells, this gets pretty darn tedious, easy to forget about, and difficult to read pretty quickly... it seems you wouldn't consider that "difficulty" and think of it as "just tediousness", but I think if something is too easy to do incorrectly and too tedious to get right, that's also a kind of added difficulty.
- dozzie 9y ago> That's not general POSIX, right? I seem to recall it's Bash-specific? (P.S. I think you forgot quotes?) Wrong. (And no, I did not.) POSIX, or rather SUS (I've never had an access to POSIX), mandates ${foo%...} syntax and its three cousins. And assignment is not subject to word splitting for variable expansion.