Understanding `echo $((0x63))`
$(...)
is a command substitution (not just a subshell), but $((...))
is an arithmetic expansion.
When you use $((...))
, the ...
will be interpreted as an arithmetic expression. This means, amongst other things, that a hexadecimal string will be interpreted as a number and converted to decimal. The whole expression will then be replaced by the numeric value that the expression evaluates to.
Like parameter expansion and command substitution, $((...))
should be quoted as to not be affected by the shell's word splitting and filename globbing.
echo "$(( 0x63 ))"
As a side note, variables occurring in an arithmetic expression do not need their $
:
$ x=030; y=30; z=0x30
$ echo "$(( x + y +x ))"
78
This is not a subshell, but arithmetic evaluation. From man bash
:
((expression))
The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let "expression".