Re:Show HN: Aviary.sh – A tiny Bash alternative to Ansible
by gravitas on Thursday June 25, 2020 @04:50PM
(#23645540)
While the other reply to you is correct, the actual answer to your question is No -- bash interprets leading 0s (zeros) in the C sense, it indicates octal not decimal. So this very small bash will fail for you:
X="08"
if [[ "$X" -eq 8 ]]; then
echo Y
fi
Use `bash -x` to run it in debug mode: $ bash -x test.sh
+ X=08
+ [[ 08 -eq 8 ]]
test.sh: line 2: [[: 08: value too great for base (error token is "08")
This comes into play if you're dealing with things like IPv4 address, where it is technically allowable to have leading zeros, however by convention it's never done because of code traps which see those leading zeros as octal mode, not decimal.
Re:Show HN: Aviary.sh – A tiny Bash alternative to Ansible
by tempodox on Friday June 26, 2020 @05:17AM
(#23650136)
Your example only demonstrates the effect of `[[`. If you replace it with `[` the error won't occur. In both cases double-quoting (or not) `$X` makes no difference in the context of your example.
Re:Show HN: Aviary.sh – A tiny Bash alternative to Ansible
by gravitas on Friday June 26, 2020 @08:32AM
(#23651238)
[ is an external program called "test" and [[ is a bash builtin, they have many different behaviours beyond just trying to use one to prove the other wrong on a random internet comment.
Re:Show HN: Aviary.sh – A tiny Bash alternative to Ansible
by tempodox on Friday June 26, 2020 @09:12AM
(#23651568)
I merely used that to show that the error message was tied to `[[` and not to the use of double quotes (which your parent comment was asking about).
Re:Show HN: Aviary.sh – A tiny Bash alternative to Ansible
by pizza234 on Thursday June 25, 2020 @03:15PM
(#23644368)
It's true, but you're semantically overloading the variable syntax (by hinting that it's treated as string).
Plus, left and single operands, in bash conditions, don't require quoting, so a lean style is actually not to use them at all.
Re:Show HN: Aviary.sh – A tiny Bash alternative to Ansible
by tempodox on Friday June 26, 2020 @06:51AM
(#23650588)
> …left and single operands…
That's false:
x='foo bar'; [ $x = 'foo bar' ]; echo $?
-bash: [: too many arguments
x='foo bar'; [ "$x" = 'foo bar' ]; echo $?
0
Re:Show HN: Aviary.sh – A tiny Bash alternative to Ansible
by gravitas on Friday June 26, 2020 @08:37AM
(#23651281)
As I replied to your other comment, you cannot just replace [[ with [ and get the same behaviour. If you use the actual bash builtin [[ which we're discussing, it works as expected. Also, please use == when comparing, = is an assignment operator (in almost every programming language).
https://www.gnu.org/software/bash/manual/html_node/Double-Qu...