The shell can represent different data in the system without invoking the 3rd party command. The following presentation method is summarized here. The default value of the shell script is handled by the 10 number, unless the number has a special notation or prefix at the beginning. Can be used to represent other types of values in the system. For example: Starting with 0 is 8. The beginning of the 0x is the 16 number. Using Base#number this form can represent other systems. Base value: 2-64.
How to use:
Other systems are converted into 10
Octal Turn decimal:
Copy Code code as follows:
[Chengmo@centos5 ~]$ ((num=0123));
[Chengmo@centos5 ~]$ Echo $num;
83
[Chengmo@centos5 ~]$ ((num=8#123));
[Chengmo@centos5 ~]$ Echo $num;
83
((expression)), (()), the inside can be any data expression. If you add: "$" before, you can read the results of the calculation.
Hexadecimal decimal:
Copy Code code as follows:
[Chengmo@centos5 ~]$ ((num=0xff));
[Chengmo@centos5 ~]$ Echo $num;
255
[Chengmo@centos5 ~]$ ((NUM=16#FF));
[Chengmo@centos5 ~]$ Echo $num;
255
base-32 Turn decimal:
Copy Code code as follows:
[Chengmo@centos5 ~]$ ((NUM=32#FFFF));
[Chengmo@centos5 ~]$ Echo $num;
507375
Base64 Turn decimal:
Copy Code code as follows:
[Chengmo@centos5 ~]$ ((num=64#abc_));
[Chengmo@centos5 ~]$ Echo $num;
2667327
Binary Turn Decimal
Copy Code code as follows:
[Chengmo@centos5 ~]$ ((num=2#11111111));
[Chengmo@centos5 ~]$ Echo $num;
255
Decimal conversion to other systems
Decimal Turn octal
Used here: BC External command complete. BC Command format conversion to: echo "obase=; value" |BC
Copy Code code as follows:
[Chengmo@centos5 ~]$ echo "obase=8;01234567" |BC
4553207
Binary, hexadecimal, base64 conversion to decimal is also the same method.
Copy Code code as follows:
[Chengmo@centos5 ~]$ echo "obase=64;123456" |BC
30 09 00
Shell, built-in various representations of the method is very simple. Remember Base#number can be. Here remember to use the (()) symbol when assigning values. You can't use the = number directly. = number has no value type. The default is to turn the string back. Such as:
Copy Code code as follows:
[Chengmo@centos5 ~]$ num=0123;
[Chengmo@centos5 ~]$ Echo $num;
0123
The beginning of 0 has lost its meaning.
You can achieve (()) an effect by using the definition character: let.
Copy Code code as follows:
[Chengmo@centos5 ~]$ let num=0123;
[Chengmo@centos5 ~]$ Echo $num;
83
There are other better ways, friends can share with me.