Why does performing a bad HEX conversion within a BASH arithmetic expression $((...)) cause all while loops to break?
Example:
#!/bin/bash
good_hex="ABCD"
bad_hex="EFGH"
while true;
do
echo "Start 1"
while true;
do
echo "Start 2"
# Convert Hex to Decimal
var1=$((16#$good_hex))
echo "Good Hex: $var1"
var2=$((16#$bad_hex))
echo "Bad Hex: $var2" # Won't be printed
echo "End 2" # Won't be printed
done
echo "Exit 2" # Won't be printed
echo "End 1" # Won't be printed
done
echo "Exit 1"
Output:
chris@ubuntu:~$ ./hex_breaks.sh
Start 1
Start 2
Good Hex: 43981
./hex_breaks.sh: line 15: 16#EFGH: value too great for base (error token is "16#EFGH")
Exit 1
After the bad hex conversion, nothing more inside either while loop is run. The next statement executed is "Exit 1" (outside of all while loops) and then the program terminates.
For comparison, using the "let" command instead of $((...)) causes the script to operate correctly and loop forever.
Example:
#!/bin/bash
good_hex="ABCD"
bad_hex="EFGH"
while true;
do
echo "Start 1"
while true;
do
echo "Start 2"
# Convert Hex to Decimal
let var1=16#$good_hex
echo "Good Hex: $var1"
let var2=16#$bad_hex
echo "Bad Hex: $var2" # Will be printed
echo "End 2" # Will be printed
done
echo "Exit 2"
echo "End 1"
done
echo "Exit 1"
Output:
chris@ubuntu:~$ ./hex_works.sh
Start 1
Start 2
Good Hex: 43981
./hex_works.sh: line 15: let: var2=16#EFGH: value too great for base (error token is "16#EFGH")
Bad Hex:
End 2
Start 2
Good Hex: 43981
./hex_works.sh: line 15: let: var2=16#EFGH: value too great for base (error token is "16#EFGH")
Bad Hex:
End 2
Start 2
Good Hex: 43981
./hex_works.sh: line 15: let: var2=16#EFGH: value too great for base (error token is "16#EFGH")
Bad Hex:
End 2
Start 2
Good Hex: 43981
./hex_works.sh: line 15: let: var2=16#EFGH: value too great for base (error token is "16#EFGH")
Bad Hex:
End 2
...
(Continues forever)
This script operates as expected and never breaks out of the while loop.
Some sources claim "let" and $((...)) are identical. My lint checker says I should use the $((...)) arithmetic compound instead of "let" because it's safer. However, breaking out of all conditional loops seems to be a completely unexpected side effect of a bad operation!
Any ideas what's going on??