I gave up Bash scripting. I explicitly do “shell scripting” now, where “shell” is implied to be a POSIX compliant shell of any type.
I’m not very acquainted with any programming language so maybe I’m wrong here (or I didn’t get the joke? XD) but bash didn’t change much in the past few years, I even read some scripts more than 10 years still works because the syntax stays the same (or doesn’t change a lot …)
Compared with the switch from python 2 -> python 3 I read a lot of people pulling their hair off xD
the joke is that bash syntax is terrible and you always forget how to do basic things in it after not touching it for a while
i forget on the same day; i literally keep a log of everything i did that day so i can look it up. lol
Okay XD it’s less funny when you have to explain the joke XD sorry !!
Here’s an example, I have looked up many times (like just now), which checks whether a string is empty:
var="" if [ -z "$var" ]; then echo "empty" else echo "not empty" fi
Why
-z
? I have no idea. I will also routinely forget the]; then
part. I believe, if you write thethen
onto the next line, then you don’t need the semicolon. And then someone’s probably gonna tell me to use double-brackets[[ ]]
instead, which probably does something.Arguably, I never fully learned Bash syntax, but it also is just a stupid if-statement. There shouldn’t be that much complexity in it.
Why -z? I have no idea.
From
man test
(note that[ <expr> ]
is just sugar fortest <expr>
):-n STRING the length of STRING is nonzero -z STRING the length of STRING is zero
So,
-z
stands for Zero.Hope this helps you remember it!
-z
means zero length and mostly[[ ]]
are used when you want to add multiple conditions. But there are also few test cases which are only in bash so they also need double bracketsYou could write that as 1 line:
[ -z "$var" ] && echo "empty" || echo "no it aint"
Configuring CORS 😭