6 ms·
What was the point of [ "x$var" = "xval" ]? (2021)
- deleted 2y ago[deleted]
- rurban 2y agoNobody used it with the quotes. The point was to get rid of the quotes. [ x$var = xval ]
- PhilipRoman 2y agoIt does nothing to help with quoting, if $var is broken then so is x$var. About half the usages I've found (and this is some pretty old code) quote the variables correctly.
- hi-v-rocknroll 2y agoThe assumption for this use was var was a valid single word but could conflict with switches to [ / test or it could be the empty string. If var was already guaranteed to be [^[:space:][:punct:][:cntrl:]]+, then x would also have been unnecessary too.
- throwaway314155 2y agoThis is discussed in the article.
- rurban 2y agoNot at all. And even our autotools are still full of x-hacks without double quotes. He completely missed the point of the x-hack. Which obviously fails with spaces.
- chasil 2y agoIf we have set var='hello world' then the syntax breaks.
- hi-v-rocknroll 2y ago[ wasn't an operator or language construct but an external program similar or the same as test. ] was a final argument. In today's world where bash is and does supplant POSIX for all intents and purposes, use [[ and (( because they're part of the interpreter and more flexible.
- chasil 2y agoNo, don't do that. You will understand why when you try this with /bin/sh on Debian/Ubuntu. This incompatibility was introduced for reasons of speed and standards compliance. There is a time and a place to rely on advanced shell features, but it should not be the default preference.
- nextaccountic 2y agoJust never do #!/bin/sh then
- hi-v-rocknroll 2y agoObsessing over POSIX purity is a pointless endeavor. It should stay in its corner, but is largely a waste of time outside of corner cases. Do not mix the two and understand the differences, but remaining locked inside a cage you make for yourself is a pointless show. The real world has bash everywhere that matters. PS: Be good, or I'll replace you with a very small zsh script. ;) Interestingly, it possible to dynamically add additional native commands to both zsh and bash in other languages. LuaBash and bash-loadables are examples.
- chasil 2y agoAdvanced shell features will not run with dash, which is (almost) strict POSIX. They also fail with busybox. There are many subtle differences between BASH and Korn, so much will not run without rewrite on Android's mksh. Because you have not needed portable scripts yourself does not imply that the need is not vast.
- 2y ago
- Leynos 2y agoOf course, I am now wondering why people are using /bin/[ in a shell script in 2024?
- deleted 2y ago[deleted]
- detourdog 2y agoBecause it makes a lot of sense in environments where sysadmins like to know what is going on. Using /bin keeps a system in sync with the platform distribution. Keeping things tied to /bin should work between upgrade cycles.
- 3np 2y agoI feel like something's missing here. I recall having to resort to the x-hack not that many years ago to acommodate for compatibility with a system shell somewhere. Beats me if I recall which and where but I'm pretty sure it had to do with empty strings.
- yesssql 2y ago>However, the value was mostly gone by the mid-to-late 1990s, and the few remaining issues were cleaned up before 2010 — shockingly late, but still over a decade ago. This is an argument for it: You never know when you'll be on a legacy system. "A decade" isn't that long.
- tedunangst 2y agoSeveral long threads in the past, too.
- jwilk 2y agoPreviously: 2023: https://news.ycombinator.com/item?id=35019232 https://news.ycombinator.com/item?id=35019232 (161 comments) 2021: https://news.ycombinator.com/item?id=26776956 https://news.ycombinator.com/item?id=26776956 (227 comments)