by lhorie · 5y ago · 0 comments
There will be people - seasoned professional developers, even - reading these languages that don't know the first thing about them!
`[` is one of those things that are second nature for seasoned bash people but that are utterly ungoogleable by a bash beginner. Beginners don't know `[` is an executable, and it would never occur to them to `man [`. It can become quite the rabbit hole to figure out that there's also `[[` (which is NOT an executable), that `[ a > b ]` is very very different from `[[ a > b ]]`, etc.
So do these people a favor and put the `if` there so they at least have some hope of stumbling upon a stack overflow post talking about some of this stuff.
https://www.gnu.org/software/bash/manual/html_node/Bourne-Sh...
They also exist as executables for ancient backwards compatibility.
"[[" is a so-called "conditional construct":
https://www.gnu.org/software/bash/manual/html_node/Condition...
I have a somewhat quirky style and almost always use "if test ..." since I find it clearer than "[" and usually don't need the additional functionality that "[[" provides.
(For similar reasons, whenever I'm grepping for a nontrivial regex, I always use egrep rather than grep, because the escaping semantics are simpler—"if it's escaped with a \, then it's not special"—and again I do not want to have the information of "which characters are special only when escaped in base grep" in my brain.)
Tangential but relevant to the Edit thread today were vis was mentioned, “No more {very-,}{no,}magic modes with different semantics and escaping rules. Instead we use the familiar and predictable POSIX Extended Regular Expression variant.”¹ is a real advantage over vim.
¹https://github.com/martanne/vis/wiki/Differences-from-Vi(m)#...
$ type -a [
[ is a shell builtin
[ is /usr/bin/[
$ type -a [[
[[ is a shell keywordBut never in scripts used in production that must be maintained by a group.
That rule served me well for many years. YMMV
>>if [ -r ~/.profile ]; then
>> source ~/.profile
>>fi
>>We can simplify this using control operators:
>>[ -r ~/.profile ] && . ~/.profile
I wonder which version I would prefer for readability three months down the road.
https://www.gnu.org/software/bash/manual/bash.html#Bash-Cond...
It is usually also a symlink to the 'test' executable but not in all Unix-like systems.