Add 'bash/booleans.bash'

This commit is contained in:
daryl
2022-05-26 14:02:40 +09:30
parent 2a20a5a7f0
commit 3b2d675ed6

23
bash/booleans.bash Normal file
View File

@@ -0,0 +1,23 @@
#!/bin/bash
# BASH doesn't actually understand booleans.
# What you can do however is to trick it by using the fact that the true and false commands return 0 and 1,
# so it can stand in for boolean algebra if used correctly.
BOOLEAN1=false
if $BOOLEAN1; then
echo "We won't get here because BOOLEAN1 is false."
fi
if ! $BOOLEAN1; then
echo "But we DO get here."
fi
BOOLEAN1=true
if $BOOLEAN1; then
echo "Now we do get in this as it's been set to true."
fi
if ! $BOOLEAN1; then
echo "And now we DON'T get here."
fi