I'm a Sr. Software Engineer at Flashpoint. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
bash 4.x is pretty good. You can do all sorts of fun things. Esp. with bash built-ins.
Lately ... I've been trying to make as many one line if statements as possible and use drop down logic for all practical purposes...too bad I can't do this with for & while loops.
You can also use this in aliases functions too, which makes setting up environments a snap...
mytag="Great big stuff"
[[ "${mytag}" == *"stuff"* ]] && { echo -en "${mytag/stuff/COOKIES}"; echo -en " are tasty\n"; sleep 5; } || { echo "Blech. You don't like cookies!"; [[ ! ${someothervar} ]] && echo "hello world."; }
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Very nice write-up. I've used all the if-constructs, except if (( ... )), so even an experience bash programmer can learn something ;-)
BTW usually I like to eliminate the use of if-statements in one-liners like this:
Where we're printing a debug message if the variable $DEBUG has a value (any value).
Simply relying on short circuit evaluation, the above can be written as:
Which I think is clearer code.
Nice, yeah the “[condition] && something || something else” is a powerful pattern when it’s clean.
I think you stole
[[ ${DEBUG} ]]
from me....bash 4.x is pretty good. You can do all sorts of fun things. Esp. with bash built-ins.
Lately ... I've been trying to make as many one line if statements as possible and use drop down logic for all practical purposes...too bad I can't do this with for & while loops.
You can also use this in aliases functions too, which makes setting up environments a snap...