3 ms·
> if [ x$(grep not_there /dev/null) = 'x' ] This is still wrong if the command can output spaces or meta-characters. You should quote the left operand, and the
by jwilk 9y ago
> if [ x$(grep not_there /dev/null) = 'x' ]
This is still wrong if the command can output spaces or meta-characters. You should quote the left operand, and then you don't need to prepend x:
if [ "$(grep not_there /dev/null)" = '' ]
- LukeShu 9y agoThe legacy of autoconf! People look at autoconf's output to learn shell, but autoconf output is full of bad practices, working around bugs in shells that noone's ever heard of. Autoconf output is expected to run on buggy shells with broken empty-string comparison. Your scripts probably aren't.
- tzs 9y agoBecause when displayed in a variable width font two single quotes can look very similar to one double quote, that last line is very easy to misread. Here it is in code mode: if [ "$(grep not_there /dev/null)" = '' ]
- gkya 9y agoDoes the second only work with bash? Because IIRC it didn't work with FreeBSD's /bin/sh, you needed the initial x too.
- jwilk 9y agoNo, this should work in any POSIX-compliant shell.