I usually don't write bash scripts. If use the terminal often to run AWS ClI commands, but if I need logic or something a bit more complex I'd rather go for a tiny NodeJs file.
It feels more natural to me, more readable and I can debug what is going wrong.
Sometimes though I have to touch old bash scripts used for continuous integration. Like today. Well, it was actually a YAML file used to run GilabCI Pipelines, but the job script contained a bash script, that wasn't working.
if [$CI_PIPELINE_SOURCE == "schedule"] && [[ -z "$STAGE" ]]; then
# collapsed multi-line command
This condition was giving an error, which was swallowed by the CI giving a successful job, but with an, of course, unexpected behavior.
27/bin/bash: line 118: [schedule: command not found
I remembered that there was something about Single Brackets or double Brackets so I immediately changed that to
[[$CI_PIPELINE_SOURCE == "schedule"]]
Nope.
Then I remembered there was something about Wrapping Variables in Quotes therefore I changed to
[["$CI_PIPELINE_SOURCE" == "schedule"]]
Nope.
I checked the docs and Stackoverflow
After googling and trying to mix and match, Single Equal / Single Equals / Quotes in every possible allowed combination I was left staring at the script and at the StackOverflow example for a while. Until it struck me.
The space.
T h e f r e a k i n g m i s s i n g s p a c e
if [[ $CI_PIPELINE_SOURCE == "schedule" ]] && [[ -z "$STAGE" ]]; then
I just repropose here the Stackoverflow explanation as my own reminder
= || == ?
Bash allows == to be used for equality with [ (but this is not standard)
To quote or not to quote?
Always wrap your variables with quotes, because if it is empty, your Bash script would encounter a syntax error. (Not because it would try to compare something with a null value, but because the syntax itself would be kind of broken.)
Hope it helps.