How to convert a shell command into an executable script Our command for the above task will be: echo $(( 2# $BINARY)) Let's assume we are supposed to write a shell command to convert the variable above to decimal knowing that the value of this variable is in base 2 (binary). For example if we have an environment variable called BINARY then we can use this variable in our shell commands like this $BINARY. When using such variables in your commands, all you require is the variable name preceeded by an $. With each environment variable, there is a variable name and a value assigned to that name. Using environment variables in shell commandsĮnvironment variables are a set of values stored within the system and available for use at any point in time within the shell. What it means is that the decimal value of 101101 base two is 45. Upon executing the above command in the shell terminal, you will get the output 45 Hence, the shell command will be: echo $(( 2#101101)) We can achieve that with the help of the echo command. To get the decimal equivalent of this, we have to make the shell display the output of this mathematical expansion. How to convert 101101 base two to base 10 in Linuxįor this example, the expression to be evaluated by arithmetic expansion will look like $((2#101101)). Assume we want to convert the number 101101 base two to decimal. The first one is the base of interest (2 in our case), followed by # then also followed by the number to be converted. So, you need three components for the expression to be evaluated through arithmetic expansion. The synthax is as such $((base_of_interest#number_to_be_converted)). So, to convert a number from one base to the base 10, we pass the number in the arithmetic expansion shortcut and categorically state the base in which it is. Hence, anytime the shell comes across this notation in a command, it is going to arithmetically evaluate the expression within it before passing the outcome to the command.īy default, Shell gives you outputs in base 10. This process is known as shell expansion.įor arithmetic expansion, the shortcut is $(( )). In simple terms, the shell understands certain shortcuts and will always interpret them before executing any commands that have such shortcuts as arguments. If you are interested in knowing more about that, then check out this article. I have already published an article on shell expansion, and I explain what arithmetic expansion is. In the shell, you can use arithmetic expansion to convert any number from one base to the other. How to convert a number from base 2 to base 10 Let's begin by taking a look at how you can do the conversion before we look at how to convert that into an executable script. Therefore, we will look at how to create a script that converts any number from base 2 to base 10, as well as one that works with the environment variable. The specific question we were given stated that the value to be converted was stored as an environment variable. 10 base-20 = 20 base-10īase-2 is equivalent to binary.In one of our Linux assignments as Software Engineering students at ALX, we were to write a script that converts a given value from base 2 to decimal (base 10). The vigesimal or base 20 numeral system is based on twenty.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |