Comment

Comment by cduzz

My biggest complaint about "idiomatic" shell scripting is the use of the [ and [[ operators. It gives the illusion that [ or [[ are part of the shell syntax when actually they're just programs / builtins / functions which communicate with the rest of the script the same way (most) other things interact -- setting exit status. Specifically this means if .. then .. fi works with any program not just [ [[ operators.

Traditional shell might be:

  grep -q thing < file
  if [ $? -eq 0 ] ; then echo "thing is there ; fi
  
VS just using if to look at the ES of the prior program

  if
    grep -q thing < file
  then
    echo "thing is there"
  fi

"test" and [[ are a fine programs / tools for evaluating strings, looking at file system permissions, doing light math, but it isn't the only way to interact with conditionals.
parent cduzz 4 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton