TIL: TIL: Bash Syntax for Inline Conditionals
Despite the last 15+ years running Ubuntu as my daily driver, I don't spend much time scripting in Bash. Yesterday while working on a git release prep skill, I found myself wondering how to compare version numbers in an inline conditional statement, without resorting to a full if[...];then...fi block.
Well, here it is:
[[ $developmentVersion == $mainVersion ]]; echo $?
In my case, this echoes 1 indicating the two values are not equal. For equal values, a 0 is echoed.
Note that the double-brace syntax ([[...]]) is Bash-specific, as is the double-equals sign (==), so make sure to use a Bash shebang in your bash scripts:
#!/bin/bash
For standard POSIX syntax, we would use:
[ "$developmentVersion" = "$mainVersion" ]; echo $?